Welcome to EMC Consulting Blogs Sign in | Join | Help

SSIS Junkie

Unambiguous dates in SQL Server

I got some smashing advice from a guy called Steve Kass today that I wanted to make sure I kept and remembered. I thought about the best place to store it and I thought, well why not right here on my blog. That way I won't forget it and maybe other people can benefit too.

Thanks for your help Steve.

====================================================================

I was having a problem with some data I was inserting into SQL Server from SSIS. I was finding that SQL Server was ambiguously interpreting the datetime strings that I was passing to it from my SSIS Execute SQL Task. (I talked about this problem previously here.)

I was passing my data in the form "YYYY-MM-DD HH:MI:SS". Depending on the language setting of the user with which I was establishing the connection, SQL Server would sometimes interprete this as "YYYY-DD-MM HH:MI:SS" which on any day after the 12 day of any month would throw an error.

Then along comes Steve and sorts out the problem for me. It turns out that the ONLY unambiguous formats for a datatime passed as a string are:

  • yyyymmdd hh:mi:ss &
  • yyyy-mm-ddThh:mi:ss

I didn't know that but I'm sure glad that I do now.

Steve also provided some sample coded to illustrate the point. paste the following into a Query Analyser (in SQL2000) or SSMS (in SQL2005) window and run it:

set language French

go

select  datename(month,s) as mnth,

        cast(s as datetime) as dt

from (

        select '2005-04-03 18:00:00' as s

        union all select '2005-04-03T18:00:00'

        union all select '20050403 18:00:00'

) T

go

set language English

go

select  datename(month,s) as mnth,

        cast(s as datetime) as dt

from (

        select '2005-04-03 18:00:00' as s

        union all select '2005-04-03T18:00:00'

        union all select '20050403 18:00:00'

) T

go

You'll get the following output

As you can see, using a format of YYYY-MM-DD HH:MI:SS can result in different results but using the two formats described above there is no such ambiguity.

========================================================

If you're reading this Steve....I'm very grateful!

-Jamie

 

 

Published 26 April 2005 22:09 by jamie.thomson

Comments

 

jamie.thomson said:

I join you in thanking Steve!

It raises some questions with me though. Why are neither of these unambiguous formats available using the CONVERT function in SQL Server? Why is there no mention of this in SQL Server BOL?
April 27, 2005 08:52
 

jamie.thomson said:

For those stuck with DTS however, neither are accepted by VBScript for conversion into a date/time. Another format which I have never had a problem with is "dd mmm yyyy [hh:mm:ss]". Generally more hassle to produced as you need the name of the month, or at least the first three characters, but it doesn't get messed up.
April 27, 2005 14:03
 

jamie.thomson said:

If you know the format your dates are coming in, you can use the SET DATEFORMAT setting. Eg: SET DATEFORMAT YMD tells SQL Server to expect dates as yyyy-mm-dd and SET DATEFORMAT YDM will set it to expect yyyy-dd-mm formatted dates.

Darren's idea to use the month name works well if you stay in the same language. As the example above shows, the month names are different in French and English.

For Paul, CONVERT(varchar(30),getdate(),126) will give the yyyy-mm-ddThh:mi:ss.mmm format. CONVERT(varchar(30),getdate(),120) will give the yyyy-mm-dd hh:mi:ss format. (I believe Steve meant to include the hyphens, but if he didn't REPLACE(CONVERT(varchar(30),getdate(),120),'-','') will work )


Region specific date formatting is always an issue, and even some Microsoft programs have issues. The SQLH2 Performance Monitor for example suffers date issues (a workaround is in the readme).
May 2, 2005 06:24
 

jamie.thomson said:

I've used the method Darren mentioned, however, I think the yyyymmdd hh:mi:ss format is easier to handle and more portable across different languages.

And no, the hiphens should not be there using this format! Try using british english and us english on the same date (such as 2005-12-26) you will see the reason why it won't work with the hiphens...
May 2, 2005 12:56
 

SSIS Junkie said:

For those of you that have not read Simon Sabin's latest blog post you need to be aware of a potential

January 16, 2007 18:08
 

Jim 2.0 said:

As promised in my original post on PL/SQL date ranges previously, below is a sql statement which can

March 1, 2007 01:46
 

SSIS Junkie said:

I read with interest Matt Masson's latest blog entry " Optional date parameter ". Its a

June 17, 2007 00:22
 

kALI CHARAN tRIPATHI said:

yyyy-mm-dd hh:mi FORMATE

SELECT CONVERT(CHAR(16), GETDATE(), 120)

December 20, 2007 17:25
 

kALI CHARAN tRIPATHI said:

This is for yyyy-mm-dd hh:mi FORMATE date formate.

this blogs is very helpfull

December 20, 2007 17:26
New Comments to this post are disabled

This Blog

Syndication

Powered by Community Server (Personal Edition), by Telligent Systems