What is wrong with this SQL Query [duplicate] - sql

This question already has answers here:
SQL - The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
(19 answers)
Closed 6 years ago.
I am doing a clean up of a database but I am getting this error. I was to delete data less than 6 months old.
Query:
Delete from dbo.Jobs
where dtpDate <= '2016-04-27 15:23:00.000'
Error:
Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
The type is datetime

Try adding a cast to the query.
CAST( '2016-04-27 15:23:00.000' AS DATETIME)

DELETE FROM dbo.Jobs
WHERE dtpDate <= CAST('2016-04-27 15:23:00.000' AS DATETIME)

Your server probably has date format settings that are incompatible with yyyy-mm-dd. Check your current dateformat setting with dbcc useroptions. If it is incorrect, then change it with set dateformat.
The best solution is to make all queries independent of the current date format, e.g.
The ODBC locale-independent escape sequence
delete from dbo.Jobs where dtpDate <= {ts'2016-04-27 15:23:00.000'}
The ODBC convert() format
delete from dbo.Jobs where dtpDate <= convert(datetime, '2016-04-27 15:23:00.000', 120)
Use parameters when issuing SQL commands from an application.

Related

Error of conversion Datetime-varchar in MSSQL

This is the query I am getting error with:
Select Time from Flights where Time <= DATEADD(day,3,'14/05/2018 00:00:00')
AND Time >= getdate()
The error says:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
This error though does not happen if I run the query on a clone-database in SQLExpress (management Studio). Any Idea? Thanks.
SQL Server likes this format (assuming standard latin collation. I can't speak to other lesser used collations):
YYYY-MM-DD
Also, Is your Time column a datetime or is it just time? That could cause another issue.
Try this query and see if it works.
Select Time from Flights where Time <= DATEADD(day,3,'2018-05-14 00:00:00')
AND Time >= getdate()

Convert from string to datatime in sql [duplicate]

This question already has answers here:
SQL - The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
(19 answers)
Closed 6 years ago.
I have a field named duedate and data type is string. I want to compare that field with current date. I used below code:
select itemid from issue where CONVERT(datetime, duedate, 105) < '20/11/2016'
i got error like "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value."
please help me out.
use this:
select itemid from issue where CONVERT(nvarchar(12), duedate, 105) < '20/11/2016'

Conversion failure with date when doing a count of result [duplicate]

This question already has answers here:
Conversion to datetime fails only on WHERE clause?
(6 answers)
Conversion failed when converting from a character string to uniqueidentifier error in SQL Server
(4 answers)
Closed 6 years ago.
I have been hitting a strange issue lately on SQL Server.
The application I am using is saving user's attributes in a table containing user/attribute/value records. I need to find whether a user is still valid using his end of validity date. The query I have been using is the following:
SELECT COUNT(*) FROM value_all
WHERE AttrName = 'VALIDTO' AND
userkey=574924 AND
CONVERT (DATE,Value, 120) < GETDATE();
The conversion is necessary because all values are stored as VARCHAR.
The query was working correctly but lately I have been hitting the following error:
SQL Error [241] [S0001]: Conversion failed when converting date and/or time from character string.
I though that the stored validity date was wrong. However, the value does match the correct format:
SELECT * FROM value_all
WHERE AttrName = 'VALIDTO' AND
userkey=574924 AND
|userkey |AttrName |Value |
|--------|-----------|--------------------|
|574924 |VALIDTO |2016-07-31T23:59:59 |
I can even do a convert and show the result correctly:
SELECT CONVERT (DATE,Value, 120) AS Date FROM value_all
WHERE AttrName = 'VALIDTO' AND
userkey=574924 AND
CONVERT (DATE,Value, 120) < GETDATE();
|Date |
|-----------|
|2016-07-31 |
However, as soon as I add the COUNT, the error is coming back.
Does any-one have an idea of what is going on in this case? Is it linked, in some way, to the value used?
EDIT
Thanks to the question at "Conversion to datetime fails only on WHERE clause?", I was able to rework my query as follow, which correct the issues I had:
SELECT COUNT(*) FROM value_all
WHERE AttrName = 'VALIDTO' AND
userkey=574924 AND
CONVERT (DATE, CASE WHEN ISDATE(Value) THEN Value END, 120) < GETDATE();
The answer is that there is no guarantee of the order in which the WHERE clause operates. It looks like it may be applying the Convert of the value field before it filters the rows on userkey or attrName. Most likely the count is causing the plan to change which is just an unlucky side effect.
You chould retrieve the row with the userkey and attrname and then convert the value.
Curious, is there an index on the userkey field? And on attrname?

SQL Data Conversion Failed DateTime [duplicate]

This question already has answers here:
Conversion failed when converting date and/or time from character string while inserting datetime
(18 answers)
Closed 6 years ago.
I have a update query as follows
UPDATE EcommerceCustomerActiveSites
SET SubscriptionExpDate = '19/08/2021 12:15:17 PM', IsEnabled = 1
WHERE ContactCode = 'CCTC-002833'
This gives me an error as follows
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
However. when I do a select query from the same table and copy a sample value from the SubscriptionExpDate column, it returns the same format as my insert statement!
19/08/2021 12:27:06 PM -- Value From Select
19/08/2021 12:15:17 PM -- Value From Insert
Does anyone have any clues?
Just use a more appropriate value for the constant string:
UPDATE EcommerceCustomerActiveSites
SET SubscriptionExpDate = '2012-08-19 12:15:17',
IsEnabled = 1
WHERE ContactCode = 'CCTC-002833';
The format for datetime constants is described in the documentation.

Update Query (String --> Date) [duplicate]

This question already has answers here:
Conversion failed when converting date and/or time from character string while inserting datetime
(18 answers)
Closed 7 years ago.
I'm trying to update field (Type of Date) from a string field.
The string field example : 20/10/2015
I am new in sql. I tried this query:
UPDATE [dbo].[EmployeeWithCompCar]
SET [EMER_AttachEndDate] = cast([EMER_info1] as date)
I get this message:
Conversion failed when converting date and/or time from character string.
Can you help me fix it?
Thank's!
You need to use convert() with a format. So try this:
UPDATE [dbo].[EmployeeWithCompCar]
SET [EMER_AttachEndDate] = convert(date, [EMER_info1], 103);
The formats are available in the documentation.
Note: In SQL Server 2012+, use try_convert() instead of convert(). That way, if a string is in the wrong format, the result is NULL instead of an error.
You can use CONVERT with the format style 103.
UPDATE [DBO].[EMPLOYEEWITHCOMPCAR]
SET [EMER_ATTACHENDDATE] = CONVERT(DATE, [EMER_INFO1], 103)
And if you don't blank to be updated to 1900-01-01 you can use NULLIF function to avoid that.
UPDATE [DBO].[EMPLOYEEWITHCOMPCAR]
SET [EMER_ATTACHENDDATE] = CONVERT(DATE, NULLIF([EMER_INFO1], ''), 103)
Date Formats in SQL Server
NULLIF