Sql server datetime is interpreted with wrong day/month - sql

I have a query that is something like
select * from myTable where [date] < 'YYYY-MM-DD'
However sql server is interpreting this in the format YYYY-DD-MM (I thought the YYYY-MM-DD format was used as it shouldn't be unambiguous, but obliviously this isn't the case!)
It would be good to know why this has happened (I haven't expereinced this before with sql server, and what the best practice is when dealing with dates.

There are three formats that are (and have been for a long time) unambiguous. These are:
YYYYMMDD
YYYY-MM-DD'T'hh:mm:ss
YYYY-MM-DD'T'hh:mm:ss.mil
Some other formats may be unambiguous on newer versions of SQL Server and/or working with the newer datetime2, date and time types, but you can always rely on the above 3. So just either use YYYYMMDD or, if possible, avoid passing strings to SQL Server when you want to deal with dates.
(Or, option 3, use CONVERT and specify exactly what format you're actually using)

I would start with two things to check:
How this date is passed to SQL Server (it can depends on date
settings on client -- i.e. Windows settings). I saw a code (in VB6)
which passes date like '2014-10-08' as 8th Oct 2014 or 10th August
2014 depends on server settings (Polish, British, American).
How SQL Server recognizes the date passed as string -- i.e. 2014-10-08 (similar issue).

Related

DATEADD uses wrong region format

For some reason specifically only the DATEADD function is using the region format yyyy-dd-mm instead of the normal yyyy-mm-dd for everything else. I don't understand why this would happen I'm not even American.
For me DATEADD(day, 7, '2021-09-01') as a column returns a datetime of 2021-01-16 00:00:00.000 not the expected 2021-09-08 00:00:00.000.
Would anyone know if this a known issue in SSMS with the DATEADD function? I am using SQL Server Management Studio v17.9.1.
This isn't a problem with SQL Server Management Studio (SSMS), which is just an IDE for SQL Server (telling us you're using SSMS 17 is like telling us you're using Office 2016 when asked what version of Windows you're using), nor SQL Server, but your literal strings. Firstly, what you should really being using DATEADD against is a strongly typed date and time value; then you always get the value you expect.
As for your literal strings, however, the format yyyy-MM-dd is not unambiguous as the way it is interpreted differs when using the (old) datetime and smalldatetime datatypes. DATEADD (and DATEDIFF) will implicitly convert a string based data type to a datetime; and such conversion is interpreted according to your language settings (meaning your format is ambiguous).
The fix is to therefore use an unambiguous format. For SQL server these are yyyyMMdd and yyyy-MM-ddThh:mm:ss.nnnnnnn. So DATEADD(day, 7, '20210901') will return 08 September 2021 regardless of your language settings.

SQL How to set Constraint date default format MM-DD-YYYY [duplicate]

At the moment I have a column that holds timestamps as a datetime
The data is being stored as 10/30/2011 10:50:34 AM
My goal is to convert every date in that specific table to be formatted like 30 Oct 2011 10:50:34
The problem:
When I attempt to run this SQL it successfully processes.
UPDATE DatesTable
SET DateTime = '30 Oct 2011 10:50:34'
WHERE DateTime = '10/30/2011 10:50:34 AM'
I noticed that was weird so if I changed the SQL to say
UPDATE DatesTable
SET DateTime = '31 Oct 2011 10:50:34'
WHERE DateTime = '10/30/2011 10:50:34 AM'
It updates updates the datetime field with the correct day/time information but keeps it in the same format.
I have looked into other possible date datatypes for that column but nothing seems to support that date.
I have also come across this SQL but am unsure if it can solve my problem.
SELECT CONVERT(datetime, datecolumnname, formattingparam) as tmp FROM tablename
Converting Link
SQL Server doesn't store a DateTime in any string format - it's stored as an 8 byte numerical value.
The various settings (language, date format) only influence how the DateTime is shown to you in SQL Server Management Studio - or how it is parsed when you attempt to convert a string to a DateTime.
There are many formats 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.
The way to solve this is to use the 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.
This is valid for SQL Server 2000 and newer.
If you use SQL Server 2008 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 data is stored independent of format. You only need to reformat dates for display purposes.
sql server doesn't store the data in a text format. It is stored as a number.
Check out this page for how to format dates in sql
http://msdn.microsoft.com/en-us/library/ms187928.aspx
The datetime datatype tells the database engine how to store the data internally, so that you don't need to worry about human representations of the data. This is why your different UPDATE scripts don't change the format. The format is only applied when converting from the database's internal storage (a number representing the amount of time passed since a particular starting-point) and a human-readable date in the culture you require. You just need to format the date when it gets displayed, in the format suitable for your users. For example, for many, Month/Day/Year makes no sense.

How convert date in SQL with respect to system culture?

Currently am using below mentioned code
CONVERT(DATETIME, "16-05-2015 21:27:25.280",105)
Using this its working in my system culture and one of our client in UK.
But facing issue in our US client.
Error message:
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value
So there any way to convert the string to DATETIME depends on system culture like in .net.
Thanks in advance
Thejus T V
The various settings (language, date format) only influence how the DateTime is shown to you in SQL Server Management Studio - or how it is parsed when you attempt to convert a string to a DateTime.
There are many formats 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.
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

SQL Server : error conversion of a varchar data type

I'm trying to run a SQL query however I'm getting an error
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Code:
SELECT
ClientId,
DatePassed,
Lender
FROM
dbo.tbl_Profile_Mortgage
WHERE
DatePassed = '2011-04-28 00:00:00.000'
I have converted the data type as so
SELECT
ClientId,
Convert (Date, DatePassed)
Lender
FROM
dbo.tbl_Profile_Mortgage
WHERE
DatePassed = '2011-04-28 00:00:00.000'
This however is still giving me the same error
Column DatePassed is of type datetime null
Any suggestions would be appreciated
Conceptually, you think the where clause is performed before the convert(). But that is not necessarily how the query is run. The only SQL statement that guarantees order of execution is case (and then not all the time).
You can try this:
SELECT ClientId,
(case when isdate(DatePassed) = 1 then Convert (Date, DatePassed) end),
Lender
FROM dbo.tbl_Profile_Mortgage
WHERE DatePassed = '2011-04-28 00:00:00.000';
EDIT:
Given that DatePassed is already in a date time format, then your error is highly unusual. This is not a problem from the conversion of this column, because no varchar is involved.
The only thing I can think of is the constant. Perhaps you have a strange setting for default dates and it is trying to get the 4th day of the 28th month. If so, you can readily fix this by putting in an explicit conversion with a format:
SELECT ClientId,
Convert(Date, DatePassed),
Lender
FROM dbo.tbl_Profile_Mortgage
WHERE DatePassed = convert(date, '2011-04-28 00:00:00.000', 121);
Aaron Bertrand has a rather extended discussion of this issue in his blog. By the way, even though I recognize the problem that he mentions with the YYYY-MM-DD format, I still use that by default. I consider it a bug that Microsoft has chosen not to recognize this ISO standard format (but just because I think it is a bug doesn't necessarily convince anyone else).
There are many formats 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. I guess in your case, you're using a language setting that just plain doesn't work with that string literal you're using.
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 concrete case - use this string in your WHERE clause:
WHERE DatePassed = '2011-04-28T00:00:00.000';
(notice the literal T between the date and time portions)

Impossible to store certain datetime formats in SQL Server

At the moment I have a column that holds timestamps as a datetime
The data is being stored as 10/30/2011 10:50:34 AM
My goal is to convert every date in that specific table to be formatted like 30 Oct 2011 10:50:34
The problem:
When I attempt to run this SQL it successfully processes.
UPDATE DatesTable
SET DateTime = '30 Oct 2011 10:50:34'
WHERE DateTime = '10/30/2011 10:50:34 AM'
I noticed that was weird so if I changed the SQL to say
UPDATE DatesTable
SET DateTime = '31 Oct 2011 10:50:34'
WHERE DateTime = '10/30/2011 10:50:34 AM'
It updates updates the datetime field with the correct day/time information but keeps it in the same format.
I have looked into other possible date datatypes for that column but nothing seems to support that date.
I have also come across this SQL but am unsure if it can solve my problem.
SELECT CONVERT(datetime, datecolumnname, formattingparam) as tmp FROM tablename
Converting Link
SQL Server doesn't store a DateTime in any string format - it's stored as an 8 byte numerical value.
The various settings (language, date format) only influence how the DateTime is shown to you in SQL Server Management Studio - or how it is parsed when you attempt to convert a string to a DateTime.
There are many formats 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.
The way to solve this is to use the 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.
This is valid for SQL Server 2000 and newer.
If you use SQL Server 2008 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 data is stored independent of format. You only need to reformat dates for display purposes.
sql server doesn't store the data in a text format. It is stored as a number.
Check out this page for how to format dates in sql
http://msdn.microsoft.com/en-us/library/ms187928.aspx
The datetime datatype tells the database engine how to store the data internally, so that you don't need to worry about human representations of the data. This is why your different UPDATE scripts don't change the format. The format is only applied when converting from the database's internal storage (a number representing the amount of time passed since a particular starting-point) and a human-readable date in the culture you require. You just need to format the date when it gets displayed, in the format suitable for your users. For example, for many, Month/Day/Year makes no sense.