Domanda

Can you please tell me difference between implicit conversion and explicit conversion in SQL Server?

I have googled about this but I can't get it.

È stato utile?

Soluzione

An explicit conversion occurs when you use the CONVERT or CAST keywords explicitly in your query.

An implicit conversion arises when you have differing datatypes in an expression and SQL Server casts them automatically according to the rules of datatype precedence.

For example nvarchar has higher precedence than varchar

CREATE TABLE Demo
(
X varchar(50) PRIMARY KEY
)

/*Explicit*/
SELECT *
FROM Demo 
WHERE CAST(X AS NVARCHAR(50)) = N'Foo'

/*Implicit*/
SELECT *
FROM Demo 
WHERE X = N'Foo' /*<-- The N prefix means nvarchar*/

The second execution plan shows a predicate of

CONVERT_IMPLICIT(nvarchar(50),[D].[dbo].[Demo].[X],0)=[@1]

Both the explicit and implicit conversions prevent an index seek in this case.

Altri suggerimenti

Implicit conversions are not visible to the user. SQL Server automatically converts the data from one data type to another. For example, when a smallint is compared to an int, the smallint is implicitly converted to int before the comparison proceeds. GETDATE() implicitly converts to date style 0. SYSDATETIME() implicitly converts to date style 21.

Explicit conversions use the CAST or CONVERT functions. The CAST and CONVERT functions convert a value (a local variable, a column, or another expression) from one data type to another. For example, the following CAST function converts the numeric value of $157.27 into a character string of '157.27':

CAST ( $157.27 AS VARCHAR(10) )

Implicit means that the database engine will convert the data type automatically, a process invisible to the user.

Explicit means that you must specify how the data type should be converted. If you don’t specify how SQL Server should convert the data types to do what you want (explicitly), it will try to guess your intentions (implicitly).

link

Implicit conversion is a conversion in which you don't have to bother about the conversion. SQL Server automatically converts the data from one data type to another. For example, if a smallint is compared to an int, the smallint is implicitly converted to int before the comparison proceeds. Suppose two column exist such as num1 in smallint and num2 in int. you want to compare them(whether equal). You have to write:

        Select ..... where num1 = num2

nothing required for the conversion.

Explicit conversion is a conversion in which you have to describe the conversion in your hand. Explicit conversions use the CAST or CONVERT functions. For example, a column date1 written in '21.01.2013'. it is in a varchar format according to the provided data/table. you want to compare with another column date2 which is in '21/01/2013' format. It is a date but provided in a varchar format as well. To compare them(whether equal) you have to write:

     select ....... where cast(date1 as date) =cast(date2 as date)   
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top