Fetch dates from SQL Server vs PostgreSQL using VDF & MERTECH - sql

I'm currently using a command on PostgreSQL at the beginning of my program to use British date style:
SET DATESTYLE TO ISO, EURO
We are testing SQL Server and I cannot find ANY equivalent function.
dbcc useroptions set language 'British', dateformat dmy;
sp_configure 'default language', 23 reconfigure with override;
SET DATEFORMAT dmy;
These are some of the things I have tested without any luck. While I see the correct values when dbcc useroptions gets queried everything remains the same; YYYYMMDD.
Can anyone point me to the right direction?

If the data type is date then set dateformat etc. does not affect how SQL Server will output it as a date - it's a date, not a string. Notice these all output YYYY-MM-DD:
DECLARE #d DATE = GETDATE();
SET DATEFORMAT DMY;
SELECT #d;
SET DATEFORMAT MDY;
SELECT #d;
SET LANGUAGE BRITISH;
SELECT #d;
SET LANGUAGE US_ENGLISH;
SELECT #d;
These settings affect how strings are interpreted, not how dates are displayed.
The answer - preferred - is to apply such formatting at the client - surely VisualDataFlex has some string formatting functions equivalent to C#'s Format() or ToString() capabilities. But still you should be careful if you want to present users with local, regional formats like d/m/y - they are bound to be misinterpreted by someone. Is 5/7/2014 May 7 or July 5? Depends on the reader.
The workaround - much less desirable - is to muck up your queries with string formatting at the server.
SELECT CONVERT(CHAR(10), col, 103) FROM dbo.tablename;

The problem is NOT # database level. As everyone pointed out the database wont convert the output.
Using Postgres the configuration line is:
datestyle = 'iso, dmy'
This take's care of the issue.
In MSSQL we had to use something else at "driver level", the command is:
SQL_USE_DATAFLEX_FORMAT set to true
And this would take care of the issue. My original question was malformed as I never said what was the main programming language and I apologize for that.
Thanks a lot to everyone as all the answers were right.

Related

SQL Server 2017 Express, unable to change datetime format from MM/DD/YYYY to DD/MM/YYYY

Installed Sql Server 2017 Express on Azure VM and wanted to change datetime format from MM/dd/YYYY to dd/MM/YYYY so for that I changed default language from us_english to British.
Edit: I am passing some dates from my application to database in DD/MM/YYYY format and database is not accepting because database has set for MM/DD/YYYY format (US format)
I tried multiple ways but none is working.
EXEC sp_configure 'default language', 23 ;
GO
RECONFIGURE ;
GO
and tried this also How to change the language and date format in SQL Server? but result is same.
Advance tab is although shows British but Datetime format is still MM/DD/YYY
The problem isn't the language of the database, or the Server, it's of the login.
You either need to provide the dates in an unambiguous format (yyyyMMdd or yyyy-MM-ddThh:mm:ss.sssssss) or change the language of the login:
ALTER LOGIN YourLogin WITH DEFAULT_LANGUAGE = BRITISH;
This query will give your languages available in Your Database...For Eg. English,German...etc..
select * from sys.syslanguages
Set Your language as your preference
SET LANGUAGE English

SQL WHERE DateTime is Between

I have this query here:
SELECT *
FROM QuestionnaireAnswer
WHERE dateCreated BETWEEN CAST('20/12/2015' AS datetime) AND CAST('22/12/2015' AS datetime)
but I keep getting this error, how come? and how do I fix it?
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
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. In your concrete case, use this WHERE clause:
WHERE dateCreated BETWEEN '20151220' AND '20151222'
and you don't even need any explicit CAST operations (or if you want to use an explicit CAST - then I'd suggest you cast to a DATE datatype - rather than DATETIME).
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
As a general rule when comparing dates in SQL Server don't use a format specific to a locale, use the universal ISO 8601 format 'yyyy/MM/dd' so the query becomes:
SELECT *
FROM QuestionnaireAnswer
WHERE dateCreated BETWEEN CAST('2015/12/20' AS datetime) AND CAST('2015/12/22' AS datetime)
You could also use one that is specific to a locale but you would have to ensure that the date format on the server always remains the same across all servers your code will connect to.
Using CONVERT you can write something like that:
SELECT *
FROM QuestionnaireAnswer
WHERE
dateCreated BETWEEN CONVERT(datetime, '20/12/2015', 103)
AND CONVERT(datetime, '22/12/2015', 103);
Check the locale settings on your server. My bet is that you are set for a US date format, but the date you have there is not in a US format.
More on querying the locale settings here.
This SO question has numerous ways to adjust the locale settings for SQL, including by user.
If you don't have permissions to change the locale for the whole server, you can change your locale within a session here. Note that SET LANGUAGE automatically sets the DATEFORMAT , but you can set the DATEFORMAT by itself.
To test this, try reversing the date and month in your date. Like so:
SELECT *
FROM QuestionnaireAnswer
WHERE dateCreated BETWEEN CAST('12/20/2015' AS datetime) AND CAST('12/22/2015' AS datetime)
Or try putting the date in a "locale insensitive format" like yyyymmdd HH:mm:ss.fff .
If column is a date field then you should do this:
SELECT *
FROM QuestionnaireAnswer
WHERE dateCreated BETWEEN TO_DATE('20-DEC-2015') AND TO_DATE('22-DEC-2015')
May not be 100% accurate depending on architecture and configuration.

SQL Date Format Day and Month Mixup

It seems this question is asked a lot, but none of the answers have given me results. I'm pulling my hair out here ... so hopefully someone has an answer.
I have a production server running SQL Server 2005. I backed up the db and restored it on my laptop's SQL Server Express instance. Now date queries are seriously affected. In the prod. server they are all stored as "4/13/2011 12:00:00 AM" format, but on my laptop they are showing as "2011-04-14 00:00:00.000". When I do a query trying to find entries on "4/14/2011" my laptop gives me the error "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value". Edit: This exact query runs fine on the production SQL server. (I'm using SSMS to run queries ... not an application/code)
I made sure my laptop's Windows regional settings are the same as the server (English(United States)) and everything on the Region and Language control panel is the exact same.
Finally I ran the following two queries:
select name ,alias, dateformat
from syslanguages
where langid =
(select value from master..sysconfigures
where comment = 'default language')
select ##language
Which gave the result of "*us_english, English, mdy*" ..... and "British" respectively. Where is this British coming from?! Now when I run this command before my query (in the management studio)
SET DATEFORMAT mdy
Then everything works perfectly! But in the syslanguages query it seems to already be mdy format. I'm not about to rewrite my application with "SET DATEFORMAT" all over the place - so hopefully someone has a clue. Maybe my SQL Express installation is buggared and I have to reinstall it?
I'm going to keep tinkering to hopefully get this to work.
It is the language settings of the login that you need to change.
You can do it through SSMS -> Security -> Logins -> YourLogin -> Properties -> Default Language
Or through TSQL
ALTER LOGIN [YourLogin] WITH DEFAULT_LANGUAGE=[us_english]
If you specify things in dd-mmm-yyyy format, or better still, use DateTime parameters (rather than varchar input), you won't have a problem.
Try changing the default language to us-english, using the following:
EXEC sp_configure 'default language', 1033
RECONFIGURE
If that doesn't work, you could try language code 0.
Are you passing dates as string literals - or concatenating strings to build up the sql in your application?
If you are doing this, consider using parameters for the dateTime data types. Or at least escape them using the ODBC escape clause and format them as in { ts'yyyy-mm-ddhh:mm:ss[.fff] '} such as: { ts'1998-09-24 10:02:20' }.
It is most probably your application not passing the datetime properly.

Accepted date format changed overnight

Since yesterday I started encountering errors related to date formats in SQL Server 2008.
Up until yesterday the following used to work.
EXEC MyStoredProc '2010-03-15 00:00:00.000'
Since yesterday I started getting out of range errors. After investigating I discovered the date as above is now being interpreted as "the 3rd of the 15th month" which will cause a out of range error.
I have been able to fix this using the following format with the "T".
EXEC MyStoredProc '2010-03-15T00:00:00.000'
By using this format its working fine. Basically all I'm trying to find out is if there is some Hotfix or patch that could have caused this, as all my queries using the first mentioned formats have been working for months.
Also this is not a setting that was changed by someone in the company as this is occurring on all SQL 2005/2008 servers
The language setting of the session can change this.
--This works
set language english
select cast('2010-03-15 00:00:00.000' as datetime)
--This doesn't
set language french
select cast('2010-03-15 00:00:00.000' as datetime)
With the T in between, it always works. If you want a space, then leave out the hyphens.
--This works
set language english
select cast('2010-03-15T00:00:00.000' as datetime)
--This works
set language french
select cast('2010-03-15T00:00:00.000' as datetime)
--This works
set language english
select cast('20100315 00:00:00.000' as datetime)
--This works
set language french
select cast('20100315 00:00:00.000' as datetime)
So my guess is that your app has changed... or some setting on your client computer.
To avoid these kind of problems you should always specify your date format. For your purposes you should use:
SET DATEFORMAT ymd;
See this MSDN page - note the comment that
ydm is not supported for date, datetime2 and datetimeoffset data types
so you might have to use a different format if you're using one of those data types.
It sounds like you've got a mixture of date styles - yyyy/MM/dd vs yyyy/dd/MM - which seem to be reverse UK and US style dates.
If all servers are showing the same behaviour it could be that just the machine executing the code has changed rather than the other machines/SQL servers in the network.
Double check that the date format or indeed culture settings are what you expect them to be on that machine. Assuming they weren't you might be able to find out what changes to the machine were made over the weekend from the event logs or Windows Update history.
Collation can effect dates, as well as the language.
To check what default language a server has installed, use the following SQL command:
sp_configure 'default language'
If the resulting value is 0, the default language U.S. English. If the result is not 0, run the following SQL command to find the installed default language setting and date format used:
select name ,alias, dateformat
from syslanguages
where langid =
(select value from master..sysconfigures
where comment = 'default language')
NOW as to the system updates updates, those were in 2007 to adjust to the DST changes mandated back then.
Machine specific: you CAN set the clock on a specific machine to NOT adjust for DST - check that setting per machine (Windows it is on Control Panel/Date and Time under XP for instance).
IF you choose to use the dashes, you may have issues in dates. If you take out the dashes, SQL Server will never misinterpret the data:
EXEC MyStoredProc '2010-03-15 00:00:00.000'
this is likely getting a time with a negative hour offset, which is in this case invalid.(just a guess)
vs
EXEC MyStoredProc '20100315 00:00:00.000'
Note that the T in there is the ISO8601 format, and thus the dashes are allowed.
Ok I just read a post over at Simons SQL Blog (http://cli.gs/hqaR4) about this specific issue. So one solution to this problem is to Use the new DATETIME2 datatype as it understands languages that doesn't use MDY dates

Error in when comparing British format date in where clause

When I run this in sql server2005 I got error.
select * from productratedates
where RateDate BETWEEN '31/10/2009' AND '03/11/2009'
Error: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
But When I run this in SQl server 2005. It is perfectly all right.
select * from productratedates
where RateDate BETWEEN '2009-10-31' AND '2009-11-03'
I want to compare date in British format.
Any help!!
Your comment says you'd like to enter dates in the day/month/year format. You can choose that format using SET DATEFORMAT:
SET DATEFORMAT dmy;
For example:
set dateformat dmy
select cast('31/10/2009' as datetime) -- succeeds
set dateformat mdy
select cast('31/10/2009' as datetime) -- fails
You can retrieve the current dateformat setting with DBCC:
dbcc useroptions
A list of available languages, with their dateformat, is available from:
exec sp_helplanguage
For me, the language called "British" has dateformat dmy. So you can change the default language for your login to British (from the property page of your login.) You can even specify it in the connection string:
Server=<server>;Uid=<login>;Pwd=<password>;Current Language=British
You could convert the date to YYYY-MM-DD format before you send it to the server.
Dates get read in US format where possible, so '31/10/2009' has to be UK format, but '03/11/2009' flips over to 11th March. That messes up your BETWEEN by going backwards in time.
I don't know if it would work for you, but we always use the format dd-mmm-yyyy:
select * from productratedates where RateDate BETWEEN '31-oct-2009' AND '03-nov-2009'
How a date is formatted is actually an interface thing. If you are looking purely at the data dates should ALWAYS be in one specific format YYYY/MM/DD.
Your interface is responsible for displaying the date in the localized format. By using this practice the script is ambiguous about where it is used and what language it is. so comparing the date should always be done in the standardized format.
What I suggest you to do is have your interface show it in the format you like and the back-end (including SQL statements) to be the standardized date format.