SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Date Puzzle


Date Puzzle

Author
Message
abmore
abmore
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1557 Visits: 931
Comments posted to this topic are about the item Date Puzzle
sharath.chalamgari
sharath.chalamgari
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1992 Visits: 798
Simple but most of the people forget when they use it. Thanks any way
Hardy21
Hardy21
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2732 Visits: 1399
Question is simple but nice one.

Thanks
henrik staun poulsen
henrik staun poulsen
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2609 Visits: 1226
'31-Jan-2010'
It that the correct (international) way to hard-code dates?

I think I would have used '2010-01-31' or is it '20100131' to ensure that it will work around the globe?



Hardy21
Hardy21
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2732 Visits: 1399
henrik staun poulsen (10/27/2010)
'31-Jan-2010'
It that the correct (international) way to hard-code dates?

I think I would have used '2010-01-31' or is it '20100131' to ensure that it will work around the globe?


date is displayed as per your database/server setting.
you need to convert the string into DATE format if you want only DATE portion (applicable for SQL 2k8 only) or DATETIME format if you want date & time both like:
SELECT CONVERT(DATE, '10/27/2010')
if you want to set variable then SELECT @dateValue = CONVERT(DATE, '10/27/2010')

Thanks
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62752 Visits: 13298
This one was too easy, but thanks for the question anyway.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Hugo Kornelis
Hugo Kornelis
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18857 Visits: 12426
henrik staun poulsen (10/27/2010)
'31-Jan-2010'
It that the correct (international) way to hard-code dates?

I think I would have used '2010-01-31' or is it '20100131' to ensure that it will work around the globe?

Hi Henrik,

A good point. Using '31-Jan-2010' in T-SQL to represent a date is not safe. If the server is set to a language other than English, you might get an error. For instance:
SET LANGUAGE French;
SELECT DATEADD(MONTH, 3, '31-Jan-2010');


Le paramètre de langue est passé à Français.

-----------------------
Msg 241, Level 16, State 1, Line 2
Échec de la conversion d'une valeur datetime à partir d'une chaîne de caractères.

(The text of these messages roughly translate to "language succesfully set to french" / "error converting character value to datetime")

For the datetime data type, The only formats that are guaranteed to work correctly in all circumstances are:
* yyyymmdd - for dates without time portion. No dashes. dots, slashes or other punctuation. The resulting datetime value will have its time portion set to midnight.
* yyyy-mm-ddThh:mm:ss - for dates with a time portion. Dashes between the date components, colons between the time components, and an uppercase T to seperate date from time.
* yyyy-mm-ddThh:mm:ss.mmm - as above, but with microseconds added. These will be rounded to the nearest 1/300 of a second.

For the new date data type, I think (too little hands-on SQL 2008 experience to be sure) that you can choose between these two formats:
* yyyymmdd - same as for datetime values, use when compatibility among date/time types is required
* yyyy-mm-dd - as presribed by ISO standards, but unfortunately not guaranteed for datetime; the new date data type finally allowed the SQL Server team to fix this (though unfortunately not for the old types - for backwards compatibility reasons, I guess)


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
archie flockhart
archie flockhart
UDP Broadcaster
UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)

Group: General Forum Members
Points: 1441 Visits: 1166
I might have had to think twice if the possible answers had been April 30th, April 31st and May 1st.
Hugo Kornelis
Hugo Kornelis
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18857 Visits: 12426
Hardy21 (10/27/2010)
henrik staun poulsen (10/27/2010)
'31-Jan-2010'
It that the correct (international) way to hard-code dates?

I think I would have used '2010-01-31' or is it '20100131' to ensure that it will work around the globe?


date is displayed as per your database/server setting.
you need to convert the string into DATE format if you want only DATE portion (applicable for SQL 2k8 only) or DATETIME format if you want date & time both like:
SELECT CONVERT(DATE, '10/27/2010')
if you want to set variable then SELECT @dateValue = CONVERT(DATE, '10/27/2010')

As far as I know (but again, my SQL 2008 experience is limited), this will fail if you have a server language (or a SET LANGUAGE) that does not use the mm/dd/yyyy format. (Which means, almost everywhere except the US).

For instance (substituting datetime for date, for I run SQL 2005):
SET LANGUAGE British;
SELECT CONVERT(DATETIME, '10/27/2010')


Changed language setting to British.

-----------------------
Msg 242, Level 16, State 3, Line 2
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.




Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
hodgy
hodgy
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2647 Visits: 596
archie flockhart (10/27/2010)
I might have had to think twice if the possible answers had been April 30th, April 31st and May 1st.


My thoughts exactly

Life: it twists and turns like a twisty turny thing

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search