SQL Server Incorrectly Parsing Date - sql-server

SQL Server 2005
SELECT TOP 10 * FROM abc WHERE aDate = '2014-01-20'
When querying the above in SSMS it would normally return results where aDate is 20 January 2014. However for another user on the same server, it returns a date conversion error and only works when running the following query:
SELECT TOP 10 * FROM abc WHERE aDate = '2014-20-01'
I've checked regional language settings on the local machine and it's exactly the same as mine. Any ideas welcomed.

It is not the regional language settings on the machine that count in this case but the one defined on the database's options.
Anyway, to avoid having to rely on the regional language settings when parsing datetime in queries I would encourage you to use an invariant ISO date format : {d 'yyyy-MM-dd'}. Note there is also one for specifying the hours (ts).

It was account specific, the setting was stored as 'British - English' as opposed to 'English'. Changing this to 'English' resolved the problem. Thank you for your responses.

This error occurred as the SQL server tries parse the date value 20 as month and it causes error as 20 is not a valid month .Always It is good practice to use the date format 'dd-MMM-yyyy' which will work with any type of SQL COLLATION and regional language settings.

Related

MS Access: How to specify a ODBC Connection String with country specific date queries?

I have severe troubles to set up an MS access application that uses linked tables to an SQL Server 2012 Database.
The problem is that SQL Queries have problems to interpret German dates: e.g. "31.12.2019" doesn't work, "01.01.2019" works. So I suspect that it is a problem with localization. E.g.
select * from table where date >= [Forms]![someForm]![fromDate]
[Forms]![someForm]![fromDate] is a string in a form, edited by a date picker.
I was able to solve the problem by using the ODBC Microsoft SQL Server Setup Wizzard, and selecting "Ländereinstellungen verwenden" (engl. use country specific settings).
(Sorry the following screenshot is in German).
I would like to specify this in a classic ODBC connection string: e.g
DRIVER=ODBC Driver 13 for SQL Server;SERVER=.\SqlExpress2012;Trusted_Connection=Yes;APP=Microsoft Office;DATABASE=suplattform;?country-specific=yes?
However I did not find such an parameter in any documentation. Is this possible?
Best regards
Michael
Also, specify the data type of the parameter - and date is a reserved word in Access SQL:
parameters [Forms]![someForm]![fromDate] DateTime;
select * from table where [date] >= [Forms]![someForm]![fromDate]

Format date of filters and results on SQL Server

I'm in a trouble with the format of dates on my queries.
In SQL Server when I execute the following query:
select *
from users
where register_date >= '2015-03-17'
It throws me a cast conversion error.
But if I do execute the following query:
select *
from users
where register_date >= '2015-17-03'
It returns me the correct data, BUT when I see the the register_date column, it gives me the dates as 'YYYY-MM-DD' format...so it's a little confusing...
How can I configure SQL Server to work always with "YYYY-MM-DD" format, on filters and results?
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 (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
So in your concrete case - what if you run this query:
select *
from users
where register_date >= '20150317'
Do you get the expected results?
Your Date format in SQL SERVER is 'YYYY-DD-MM' that's why you are getting cast conversion error. For searching you must give the date with this exact format.
But In case of getting result you can use SQL DATE FORMAT and retrieve date in any format e.g
SELECT DATE_FORMAT(users.register_date ,'%Y-%d-%m') AS date FROM users
Result will be : 2015-17-03

Search By Date in SQL Server 2012

We just upgraded to SQL Server 2012 from 2005. While I'm a novice, something this simple couldn't be this difficult. I used to be able to pull data from a table based on the date vs date and time. As it now stands I have:
Select * from receipts_table where receipt_cancel_date = '2013-09-20'
before we upgraded this would work fine. How can I run this and actually get the desired results as I know there's receipts with a cancel date of 2013-09-20.
Thanx
If you are passing string for a date parameter, best format is ISO (yyyymmdd) format. Otherwise even though your string work in some servers it might not work in another depending on the culture of the server. ISO format is culture independent.
Also remove the time part from receipt_cancel_date column by converting it to a DATE (if DATETIME) for comparison purpose.
Try this:
Select * from receipts_table
where convert(date, receipt_cancel_date) = convert(date,'20130920')
Or use 120 style with your format:
Select * from receipts_table
where convert(date, receipt_cancel_date) = convert(date,'2013-09-20',120)

CAST failing to create date

I'm getting a cast failure and I can't fathom why.
Here's what is failing :
select cast('16/04/2012' as datetime)
The error is :
"The conversion of a char data type to a datetime data type resulted
in an out-of-range datetime value."
If I use CONVERT with a 103 for format it works without issue as you would expect.
The server is set to british date format, therefore the MSSQL account should also be defaulting to britsh format. It's been rebooted, so it shouldn't be that the service is using a different date format.
This is a SQL2005 instance.
What I really want to know is, what could be causing the CAST to fail?
Use this code:
SELECT convert(datetime, '16/04/2012',105)
Have you tried SET DATEFORMAT DMY?
Date format and datetime format are not necessarily the same. While it may implicitly add the 00:00:00 for hh:mm:ss, maybe try adding that. Type 103 only includes dd/mm/yyyy, so it of course works.
You have a data format as MM:dd:YY, and cast tries to convert you '16/04/2012' which is 'dd/MM/yy' and throws exception because 16 is less then 12 monthes.
You can either change your data format in server settings or use SET DATEFORMAT statement before your query
Are you sure the server is British language format? If I run:
set language british
select cast('16/04/2012' as datetime)
Then I get:
2012-04-16 00:00:00.000
You can check the current session language with
select ##language
The session language defaults from the login in use, assuming it's a SQL Server-provisioned login (i.e. not a Windows user). To check the language for a given user:
select loginproperty('myuser', 'DefaultLanguage')
To make a permanent server change for all newly created logins:
EXEC sp_configure 'default language', 23
reconfigure
...where 23 is the langid obtained via sp_helplanguage.
Always use a language neutral date format - it will always work regardless of any settings:
CAST('YYYYMMDD', AS DATE);
4 digit year, 2 digit month, 2 digit day and NO separators. You will never again have to worry about default language settings.

Error converting string to datetime due to locale

I'm having a lot of difficulty with locale's in a particular instance of SQL Server 2008 R2 Express.
I'm in the UK and the following fails:
SELECT CAST('2012-12-31' AS DATETIME)
Error message:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
The Windows server locale is British English. My login locale is British English. Collation 'if that matters' is Latin1_General_CI_AS.
The database 'language' is English (United States) but then this is the same as another instance on a different server and the above SQL doesnt fail.
Any thoughts?
For the user making the database connection -- the SQL user -- set the language to English.
This is a setting specific to the SQL user of the connection issuing the query
One way to check if this is a problem... Run this in Management Studio and login as the SQL user who issues the query
SET LANGUAGE English
SELECT CAST('2012-12-31' AS DATETIME)
If this works, set the default language of the SQL user appropriately
Don't use YYYY-MM-DD for date literals, always use YYYYMMDD. This will never fail, regardless of locale, dateformat settings, language settings, regional settings, etc:
SELECT CAST('20121231' AS DATETIME);
A worthwhile read perhaps:
Bad habits to kick : mis-handling date / range queries
You should explicitly define the date format on your convert, in this case is 120:
SELECT CONVERT(DATETIME,'2012-12-31',120)
You can take a look at this page to see more date formats:
http://msdn.microsoft.com/en-us/library/ms187928.aspx

Resources