Difference between Implicit Conversion and Explicit Conversion in SQL Server [duplicate] - sql

This question already has answers here:
SQL Server and implicit conversion of types
(2 answers)
Closed 8 years ago.
What is the difference between implicit conversion and explicit conversion in SQL Server?

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.

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)

Related

sql server - error converting float to varchar

I have a float field, I need to convert it as varchar and I need to add a leading zero when it contains only one nummber(it can contains 1 or 2 numbers). This is my query:
SELECT FORMAT(((LTRIM(RTRIM(STR(floatField,2))))), '00')
but I get an error:data type for argument 1 is not valid. I know I can do this in a different way, but I want to understand what's wrong in this
format takes a float (among other things) and converts it to a varchar. Don't try to do its job for it:
SELECT FORMAT(myfloat, '00')
FROM mytable
DBFiddle Demo

Why sysdate-'dd-mm-yyy' is not valid in-spite of internal conversion being possible?

'dd-mm-yy' being NLS_DATE_FORMAT it is implicitly converted to Date data type during comparison, insertion but why is not converted during a arithmetic operation.
sysdate>'01-01-17' //is valid
sysdate-'01-01-17' //is in valid
First I assumed the operators(+,-,..) are only for numeric data type. Later I got to know these operators are used even in Date Arithmetic and even operands with Datedata type are also valid.
"During arithmetic operations on and comparisons between character and noncharacter datatypes, Oracle converts from any character datatype to a numeric, date, or rowid, as appropriate" -
doc
Using to_date solves the issue. I am looking for the reason why it is not implicitly converted.
Forget implicit conversion. Just express your dates using explicit date literals:
sysdate > date '2017-01-01'
sysdate - date '2017-01-01'
The code is clearer and less ambiguous as well.
As to why Oracle doesn't do implicit conversion in the second case. Oracle doesn't know what type to expect. The second operand could be either a date or a number, so it doesn't know how to convert the string. In the first case, the comparison should be to a date.
Adding more detail on Gordon Lindoff's answer with an example.
During sysdate>'010117' as your comparing with a date '010117' surely should be date and is implicitly converted. Same going during insert.
But during sysdate-'010117' the system has the possibilities of converting it Number or Date, and it chooses to convert into Number. So 'dd-mm-yy' format is tried to convert into Number in this context.

Finding row causing error in type conversion in SQL Server

I am trying to cast a column as smalldatetime from varchar. There are some rows containing errors, how can I find them?
SELECT
PA.EAR_TAG
,ISNULL(B.DISPOSAL_DATE, H.DISPOSAL_DATE) as HB_Date
,Y.[DATE OF MOVEMENT] as Y_Date
,DATEDIFF(DAY, ISNULL(B.DISPOSAL_DATE, H.DISPOSAL_DATE), cast(Y.[DATE OF MOVEMENT] as smalldatetime))
FROM
DairyTelomere.dbo.PROJECT_ANIMALS AS PA
LEFT JOIN
Langhill.dbo.YOUNG_STOCK_BULL AS B ON Pa.EAR_TAG = B.EAR_TAG
LEFT JOIN
Langhill.dbo.YOUNG_STOCK_HEIFER AS H ON PA.EAR_TAG = H.EAR_TAG
LEFT JOIN
DairyTelomere.dbo.Young_Stock_culls AS Y ON PA.EAR_TAG = Y.Ear_Tag
The error I get is:
Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value.
I know that if the column was in a date format I could check it by using ISDATE() but unfortunately I can't change the column type (don't have permissions).
Any ideas will be greatly appreciated.
you can use the isdate to get a list of all the ones that are not converting for you. You dont need to change the column type to use this so i am confused by your statement
if the column was in a date format I could check it by using ISDATE() but unfortunately I can't change the column type (don't have permissions)
Will help more if you can clarify but this query should get you a list of rows that have bad date values.
select table.date_as_varchar
from table
where isdate(table.date_as_varchar) = 0
adding to #workabyte answer you can also try using TRY_PARSE, TRY_CAST OR TRY_CONVERT, all of them return NULL if the conversion failed, that way you can know wich rows caused the error.
TRY_PARSE
as the documentation says:
Use TRY_PARSE only for converting from string to date/time and number
types. For general type conversions, continue to use CAST or CONVERT.
Keep in mind that there is a certain performance overhead in parsing
the string value.
Usage example:
SELECT TRY_PARSE(your_date AS DATETIME USING 'es-ES') as date
FROM your_table
es-ES is a culture parameter,different culture paramaters yield different results in your conversions, you can find the full list of parameters in the documentation
TRY_CONVERT
as the documentation says:
TRY_CONVERT takes the value passed to it and tries to convert it to
the specified data_type. If the cast succeeds, TRY_CONVERT returns the
value as the specified data_type; if an error occurs, null is
returned. However if you request a conversion that is explicitly not
permitted, then TRY_CONVERT fails with an error.
Usage example:
SELECT TRY_CONVERT(DATETIME,your_date,103) as date
FROM your_table
The 103 being the style/format of the date you are converting, here you can find a list of the formats avaliable
TRY_CAST
as the documentation says:
TRY_CAST takes the value passed to it and tries to convert it to the
specified data_type. If the cast succeeds, TRY_CAST returns the value
as the specified data_type; if an error occurs, null is returned.
However if you request a conversion that is explicitly not permitted,
then TRY_CAST fails with an error.
Usage example:
SELECT TRY_CAST(your_date AS DATETIME) as date
FROM your_table

Like operator in Datetime column

I want to use Like operator in a column of datetime. The values of the column is as follows:
2013-08-31 17:54:52.000
2013-08-31 17:54:52.000
My query is as below:
SELECT * FROM table where created_date Like '%54%'
It works fine. but when I search for '%52%' instead of '%54%', it gives me nothing. (It is working when I search till the minutes, but when I search for seconds or milli seconds it does not work.)
I have looked at the following url and it is working
SQL LIKE Statement on a DateTime Type
I want to know the reason, why this is happening and how like operator works with datetime type column.
I think it would be a better idea to use the DATEPART operator of SQL SERVER to extract the portion of date.
And example could be like:-
SELECT * FROM table
where DATEPART(minute,created_date)=54
EDIT:-
I want to know the reason, why this is happening and how like operator
works with datetime type column.
Actually there is no direct support given by SQL Server for LIKE operator for DATETIME variable but you can always cast the DATETIME to a VARCHAR and then try to use the LIKE operator as you want.
On a side note:-
MSDN says:-
DATEPART can be used in the select list, WHERE, HAVING, GROUP BY and
ORDER BY clauses.
In SQL Server 2012, DATEPART implicitly casts string literals as a
datetime2 type. This means that DATEPART does not support the format
YDM when the date is passed as a string. You must explicitly cast the
string to a datetime or smalldatetime type to use the YDM format.
The 'LIKE' operator and any regular expression operators provided by other databases are used to process text values. A date is definitely not a text value, it is a separata type by itself.
It makes little sense to apply a text operator to a non-text type (like int or DATETIME or DATETIMEOFFSET), which is why you can't use LIKE on dates in any database. First of all, the values are not stored as text but in an implementation-specific binary form.
Then, while you can use LIKE on a specific text representation of a date, eg using CAST you have to absolutely certain what that representation is. Different locales display dates differently, with year first, year last, month first or last or whatever. What would you search against?
Moreover, what is 54? 54 minutes, 54 seconds or 654 milliseconds?
The only sensible solution is to use DATEPART to check specific parts of a date, or the BETWEEN operator to check for ranges.

mysql datetime comparison

For example the following query works fine:
SELECT *
FROM quotes
WHERE expires_at <= '2010-10-15 10:00:00';
But this is obviously performing a 'string' comparison - I was wondering if there was a function built in to MySQL that specifically does 'datetime' comparisons.
...this is obviously performing a 'string' comparison
No - if the date/time format matches the supported format, MySQL performs implicit conversion to convert the value to a DATETIME, based on the column it is being compared to. Same thing happens with:
WHERE int_column = '1'
...where the string value of "1" is converted to an INTeger because int_column's data type is INT, not CHAR/VARCHAR/TEXT.
If you want to explicitly convert the string to a DATETIME, the STR_TO_DATE function would be the best choice:
WHERE expires_at <= STR_TO_DATE('2010-10-15 10:00:00', '%Y-%m-%d %H:%i:%s')
But this is obviously performing a 'string' comparison
No. The string will be automatically cast into a DATETIME value.
See 11.2. Type Conversion in Expression Evaluation.
When an operator is used with operands of different types, type conversion occurs to make the operands compatible. Some conversions occur implicitly. For example, MySQL automatically converts numbers to strings as necessary, and vice versa.
I know its pretty old but I just encounter the problem and there is what I saw in the SQL doc :
[For best results when using BETWEEN with date or time values,] use CAST() to explicitly convert the values to the desired data type. Examples: If you compare a DATETIME to two DATE values, convert the DATE values to DATETIME values. If you use a string constant such as '2001-1-1' in a comparison to a DATE, cast the string to a DATE.
I assume it's better to use STR_TO_DATE since they took the time to make a function just for that and also the fact that i found this in the BETWEEN doc...