SQL Server changing date format to accept d/m/y - sql-server

An application is passing the below query to the SQL server and I'm receiving an exception from SQL server as The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
update Images set Created_DATE='23/09/2020 11:00:09'
where ID = 10
Additionally, I cant see the below error in Profiler.
What I've tried is,
Changes the Date format of SQL server as DMY
Change the language to en-GB
I can't change the code so how to make this work by changing SQL server configuration?

Try this:
update Images
set Created_DATE=CONVERT(DATETIME, N'23/09/2020 11:00:09', 103)
where ID = 10
You need to convert the string to date setting a culture. Otherwise, the engine is not able to understand the format as there are various formats.
I believe, your date type is DATETIME, and that's why you are getting this error:
SELECT CAST('23/09/2020 11:00:09' AS DATETIME);
result as:
Msg 242, Level 16, State 3, Line 4 The conversion of a varchar data
type to a datetime data type resulted in an out-of-range value.
for DATETIME2 it is:
Msg 241, Level 16, State 1, Line 4 Conversion failed when converting
date and/or time from character string.

Related

How to convert it to a date format in SQL Server

I am trying to convert this time 2021-08-16 12:58:00.000 to a DateTime this way:
SELECT Convert(datetime, '16/08/2021 12:58:00:000PM');
And getting "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.".
What am I doing wrong? Is it an invalid date time?
The issue is that your date is formatted at dd/mm/yyyy and SQL Server expects it as mm/dd/yyyy. In fact, if you do this it will succeed:
SELECT CONVERT(datetime, '08/16/2021 12:58:00:000PM');
To make it work for your format, you need to pass a style value to let SQL Server know the format you are using. For your example, this works:
SELECT CONVERT(datetime, '16/08/2021 12:58:00:000PM', 103);
103 corresponds to dd/mm/yyyy. You can see a full list of style values in the documentation at https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-ver15

T-SQL BULK INSERT type mismatch

I am trying to do a simple BULK INSERT from a large CSV file to a table. The table and the file have matching columns. This is my code:
BULK INSERT myTable
FROM 'G:\Tests\mySource.csv'
WITH (
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
-- ROWTERMINATOR = '0x0a',
BATCHSIZE = 1000,
MAXERRORS = 2
)
GO
As you can see I have tried with row terminators \n and 0x0a (and a bunch more)
I keep getting a type mismatch error:
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 2, column 18 (createdAt).
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 3, column 18 (createdAt).
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 4, column 18 (createdAt).
Msg 4865, Level 16, State 1, Line 1
Cannot bulk load because the maximum number of errors (2) was exceeded.
Msg 7399, Level 16, State 1, Line 1
The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.
Msg 7330, Level 16, State 2, Line 1
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".
Column createdAt is of type datetime:
CREATE TABLE [dbo].[myTable]
(
...
[createdAt] [datetime] NULL,
...
)
These are the values of the createdAt column as taken from the first three rows:
2020-08-22 13:51:57
2020-08-22 14:13:13
2020-08-22 14:16:23
I also tried with a different number format as suggested. I also tried changing the column type to DATETIME2(n):
2020-08-22T13:51:57
2020-08-22T14:13:13
2020-08-22T14:16:23
I have no idea what else to review.
I would appreciate any help.
Thanks!
There are many formats of string literals to be converted to dates & times supported by SQL Server - see the MSDN Books Online on CAST and CONVERT. Most of those formats are dependent on what settings you have - therefore, these settings might work some times - and sometimes not. And the DATETIME datatype in particular is notoriously picky about what formats of string literals work - and which others (most) don't.... DATETIME2(n) is much more forgiving and less picky to deal with!
The way to solve this is to use the (slightly adapted) ISO-8601 date format that is supported by SQL Server - this format works always - regardless of your SQL Server language and dateformat settings.
The ISO-8601 format is supported by SQL Server comes in two flavors:
YYYYMMDD for just dates (no time portion); note here: no dashes!, that's very important! YYYY-MM-DD is NOT independent of the dateformat settings in your SQL Server and will NOT work in all situations!
or:
YYYY-MM-DDTHH:MM:SS for dates and times - note here: this format has dashes (but they can be omitted), and a fixed T as delimiter between the date and time portion of your DATETIME.
This is valid for SQL Server 2000 and newer.
If you use SQL Server 2008 or newer and the DATE datatype (only DATE - not DATETIME!), then you can indeed also use the YYYY-MM-DD format and that will work, too, with any settings in your SQL Server.
Don't ask me why this whole topic is so tricky and somewhat confusing - that's just the way it is. But with the YYYYMMDD format, you should be fine for any version of SQL Server and for any language and dateformat setting in your SQL Server.
The recommendation for SQL Server 2008 and newer is to use DATE if you only need the date portion, and DATETIME2(n) when you need both date and time. You should try to start phasing out the DATETIME datatype if ever possible
In your case, I'd try one of two things:
if you can - use DATETIME2(n) instead of DATETIME as your column's datatype - that alone might solve all your problems
if you can't use DATETIME2(n) - try to use 2020-08-22T13:51:57 instead of
2020-08-22 13:51:57 for specifying your date&time in the CSV import file.

Load the date separated by '/' with the Datetime datatype in sql server

INSERT INTO PUZ_DATE_FORMAT
SELECT FORMAT(GETDATE(),'d', 'it-IT') AS ItalianDate
I get this error:
Msg 242, Level 16, State 3, Line 1
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
My table contains only a single column of datetime datatype, and the format statement which I've written will give the output like 15/12/2017.
But when I try to insert a row into the table, it won't allow me to do so. It allows only dd-mm-yyyy format - not dd/mm/yyyy. Why?
Basically what you are trying to do is insert a string value to a Datetime field. In doing so, SQL Server try to cast the string to a datetime during the insert. If your string is in the Universal format (yyyy-MM-dd) it can successfully parse it without any issue. Else the string has to be in the datetime format as Server's culture.
I guess your Server's culture is en-US which has the date time format as "MM/dd/yyyy". With regarding to your date (15/12/2017) server thinks 15 is the month, 12 is the day and so on. Which is obviously falling.
If you tried this before 12th of the month, It can successfully cast, But to an incorrect value.
Further, you are not doing the right thing by, casting a Datetime to a string and then try to insert in to Datetime field. Try to store the raw Datetime in database and format accordingly when displaying in UIs.
Cheers,

How to solve "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value." error

select top 10
FormANo, Created, Changed
from
FormA
where
Created >= convert(datetime, '2015-07-05 14:04:11.000')
and Created <= convert(datetime, '2016-04-21 20:13:08.280')
when I run the query I am getting the following error
Msg 242, Level 16, State 3, Line 4
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
How can I solve this using raw SQL query in SQL Server 2014?
Most likely SQL Server is trying to parse your data in another format that you are providing.
You can set the format with one of the values from this table:
convert(datetime, '2016-04-21 20:13:08.280', 121)
121 = yyyy-mm-dd hh:mi:ss.mmm(24h)

SQL Server date Format user dependent?

I have a query with hard coded dates, in this format
startdate >= '2012-11-03' AND enddate <= '2012-11-30 23:59'
My database date format is 'mdy', however I'm sure it will accept yyyy-mm-dd as its the universal date structure.
When I try run this query in SSMS on my target DB connected with a specific database user (userX) I get an error about the date formats
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.
However, when I run the exact query connected as the SA user, the query executes..
Why is this? I have given userX full dbo permissions (sysadmin etc) and still get the error?
If you need to specify datetimes using strings, you should use a safe, language-independent format.
In SQL Server, that's the ISO-8601 format (slightly adapted), and it supports basically two safe formats for DATETIME that always work - regardless of your language, regional and dateformat settings:
YYYYMMDD (e.g. 20121231 for 31st of December 2012) if you need date only
YYYY-MM-DDTHH:mm:ss (e.g. 2012-12-31T21:05:00 for 31st of December 2012, 9:05pm)
Note:
the first date-only format has no dashes or delimiters!
the second format has dashes for the date (can be omitted, too), and there's a fixed T as delimiter between the date and the time portion of the string
Update: as per your last comment (on the different default languages for the two users) - try this:
-- this is how your `SA` interprets the string as datetime....
SET LANGUAGE english
SELECT CAST('2012-11-30 23:59' AS DATETIME)
Works just fine...
-- this is how your British user interprets teh string as datetime
SET LANGUAGE british
SELECT CAST('2012-11-30 23:59' AS DATETIME)
Msg 242, Level 16, State 3, Line 7
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
This tries to interpret the string as 11th of the 30th month of 2012 and obviously, that fails....
consider using
startdate >= '2012-11-03' AND enddate < '2012-12-01'
instead

Resources