SET DATEFORMAT, a thing to remember.

Today I faced with an issue that an older SQL 2000 server (source db) had different formatting settings (my guess is the OS’s regional settings) than my DWH server (target db) or my local machine. Googled a bit and found the ‘SET DATEFORMAT‘-statement:
SET DATEFORMAT { dym | dmy | mdy | myd | ydm | ymd }

An easy method to check which format setting the server is applying/using, is to execute this SQL query:
SELECT DatePart(Month, '11-7-2014') AS CurrentMonth


How it works

SET DATEFORMAT mdy
GO
DECLARE @datevar datetime
SET @datevar = '07/11/14'
SELECT @datevar AS CurrentDate
GO

SET DATEFORMAT ydm
GO
DECLARE @datevar datetime
SET @datevar = '14/11/07'
SELECT @datevar AS CurrentDate
GO

SET DATEFORMAT ymd
GO
DECLARE @datevar datetime
SET @datevar = '14/07/11'
SELECT @datevar AS CurrentDate
GO

The results:

sql-set-dateformat-results








Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s