Insert it as '2012-11-16T00:00:00.000'
if you don’t need the time, insert it as
'20121116'
Those are the ISO formats for datetime/date respectively.
As with all other formats, you get problems depending on the language of the user that the SQL statement runs at.
You can also get problems like that with months/days, and it’s not always apparent.
e.g. if you parse a string like '01/01/2015'
, it will always work.
But when you get to '01/13/2015'
, you run into problems, because English uses month/day/year
, while for example German uses day/month/year
, and 13 is not a valid month…
In your case, it’s somehow misinterpreting time, and that can lead to all kinds of strange errors.
VALUES (convert(varchar,convert(datetime,{D '2019-06-30'}),102));
Starting from the inside, the ODBC escape sequence {D '2019-06-30'}
returns a datetime
.
(ignoring the redundant convert to datetime
)
You’re then converting that to a string with 102 style (rather than 105 for Italian).
You’re then relying on an implicit conversion back to datetime
to match the type of the target column.
The implicit conversion has a default style of 0 as you can see in the execution plan:
[Expr1003] = Scalar Operator(CONVERT_IMPLICIT(datetime,CONVERT(varchar(30),[@1],102),0))
(note: you should always specify the maximum length when using varchar
)
When you use style 102 yyyy.mm.dd
you must also set DATEFORMAT
to YMD
so SQL Server can parse the format under style 0.
When you use style 105 dd-mm-yyyy
, you must set DATEFORMAT
to DMY
for the same reason.
The reason it works on one and not the other is the default DATEFORMAT
for the language in each case.
See datetime, SET LANGUAGE
, and Write International Transact-SQL Statements in the documentation.
Applications that use other APIs, or Transact-SQL scripts, stored procedures, and triggers should use the CONVERT statement with an explicit style parameter for all conversions between the time, date, smalldate, datetime, datetime2, and datetimeoffset data types and character string data types.
Also SQL Server DateTime Best Practices by Aaron Bertrand.
Home > SQL Server Error Messages > Msg 242 — The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. |
||
SQL Server Error Messages — Msg 242 — The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. |
||
To illustrate, the following SELECT statements will generate the error: SELECT CAST('02/29/2006' AS DATETIME) -- 2006 Not a Leap Year SELECT CAST('06/31/2006' AS DATETIME) -- June only has 30 Days SELECT CAST('13/31/2006' AS DATETIME) -- There are only 12 Months SELECT CAST('01/01/1600' AS DATETIME) -- Year is Before 1753 Another way the error may be encountered is when the format of the date string does not conform to the format expected by SQL Server as set in the SET DATEFORMAT command. To illustrate, if the date format expected by SQL Server is in the MM-DD-YYYY format, the following statement will generate the error: SELECT CAST('31-01-2006' AS DATETIME) Solution / Work Around: To avoid this error from happening, you can check first to determine if a certain date in a string format is valid using the ISDATE function. The ISDATE function determines if a certain expression is a valid date. So if you have a table where one of the columns contains date values but the column is defined as VARCHAR data type, you can do the following query to identify the invalid dates: SELECT * FROM [dbo].[CustomerTransactions] WHERE ISDATE([TranDate]) = 0 Once the invalid dates have been identified, you can have them fixed manually then you can use the CAST function to convert the date values into DATETIME data type: SELECT CAST([TranDate] AS DATETIME) AS [TranDate] FROM [dbo].[CustomerTransactions] Another way to do this without having to update the table and simply return a NULL value for the invalid dates is to use a CASE condition: SELECT CASE ISDATE([TranDate]) WHEN 0 THEN CAST([TranDate] AS DATETIME) ELSE CAST(NULL AS DATETIME) END AS [TranDate] FROM [dbo].[CustomerTransactions] |
||
Related Articles : | ||
|
Hello everyone,
I have a strange issue here and i really do not know what i can do about it. We have a batch running every day on the SQL server that creates a new Job on the SQL server. This Job is scheduled to kickoff a DTS package and inside this DTS package there are
several stored procedures that will run and fill the tables with data.
Now this batch is running fine for about a year long now, until a week ago. Now this Job gives me an error message everytime it runs a specific stored procedure inside the DTS package. The error message is:
Server: Msg 242, Level 16, State 3, Line 1 The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
Now the query where this happens is the following query:
insert into ShP_VBC_RAP_Verwerkt_Per_Medewerker
(
Datum, Medewerker, Aantal, Status
)
select convert(varchar, created,101) datum, medewerker, count(*) aantal, status
from ShP_VBC_OudeWerklijst
group by medewerker, status, convert(varchar, created,101)
order by medewerker, status asc
Now this is just a query to fill some data that is used for reports and it is not really a complicated query. The ‘created’ and ‘Datum’ fields are DateTime fields inside the tables.
The strange part of this story is that when i run this query inside the Query Analyzer it runs fine, if i run the specific action inside the DTS package (for this query) it runs fine. The error message only happens when i run it in combination with the created
SQL Job…
Does anybody have any idea why this error only happens during my created Job and maybe what i can do about it?
Many thanks in advance!!
Vincent
- Remove From My Forums
-
Вопрос
-
We have one SQL Job, custom written for copying data from multiple tables across different SQL Server to a table in central SQL Server. The job was running properly for sometime. From few days, we have been noticing the Job fails with the following
error.The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. [SQLSTATE 22007] (Error 242) The statement has been terminated. [SQLSTATE 01000] (Error 3621). The step failed.
Can anyone suggest what could be the issue with the SQL Job?
Regards, K Phani Kumar IT Analyst Windows & Messaging Tata Consultancy Services
Ответы
-
check datetime format in all servers should be same… lets suppose you are converting some character data to datetime datatype and from one server it is getting right format to convert in datetime but from other server it is not getting right format
like
if
select convert(datetime ,’2005-12-18 21:28:00′,120)
is working
but select convert(datetime ,’2005-18-12 21:28:00′,120) will not work
due to there is no possibility of 18 months in a year so check this issue
-
Помечено в качестве ответа
28 июля 2010 г. 10:48
-
Помечено в качестве ответа