Well... if you're going to do that, then I would suggest that you modify the format. There are only 2 un-ambiguous date formats that you can use with SQL Server.
YYYYMMDD hh:mm:ss.mmm
YYYY-MM-DDThh:mm:ss.mmm
Your example date should appear as:
20080421 14:52:48.443 or
2008-04-21T14:52:48.443
Every login for SQL Server has a language associated with it. Each language has a dateformat associated with it. The DateFormat determines how dates are interpreted by SQL Server. The formats mentioned above are unambiguous, meaning that they will ALWAYS be interpreted the same way, no matter what language/dateformat is specified.
Now, you may be thinking, that will never affect me. However, you should consider doing this anyway for 2 reasons.
1. You just never know what your app will be used for in the future.
2. It's easy to change the date format for a string when passing it to SQL Server.
The following languages use a Date-Month-Year format.
[tt][blue]German
French
Danish
Spanish
Italian
Dutch
Norwegian
Portuguese
Finnish
Czech
Polish
Romanian
Slovak
Slovenian
Greek
Bulgarian
Russian
Turkish
British English
Estonian
Brazilian
Arabic
Thai[/blue][/tt]
For a complete list, run this (in Query Analyzer).
[tt][blue]sp_HelpLanguage[/blue][/tt]
To see how the date format affects SQL Server's interpretation of the date....
Code:
declare @Temp varchar(30)
Set @Temp = '[!]2008-04-21 14:52:48.443[/!]'
Set DateFormat DMY
Select Cast(@Temp As DateTime)
When you run that code (in Query Analyzer), you will get this error message:
[red]The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.[/red]
This occurs because SQL Server is interpreting the date format to mean, 4th day of 21st month in the year 2008. Since there is no 21st month, you get an error.
Now, run these.
Code:
declare @Temp varchar(30)
Set @Temp = '[!]20080421 14:52:48.443[/!]'
Set DateFormat DMY
Select Cast(@Temp As DateTime)
Code:
declare @Temp varchar(30)
Set @Temp = '[!]2008-04-21T14:52:48.443[/!]'
Set DateFormat DMY
Select Cast(@Temp As DateTime)
The last two execute as you would expect it to.
Now, don't mistakenly think that your original format is ok simply because you do NOT use Set DateFormat within stored procedures. This is simply not right. Like I said earlier, if you have a login that has a language set to Spanish (for example), even if you don't have Set Dateformat in your stored procedure, you will still have a problem.
Code:
declare @Temp varchar(30)
Set @Temp = '2008-04-21 14:52:48.443'
Set Language 'Spanish'
Select Cast(@Temp As DateTime)
If your login has it's language set to Spanish, you do not need to use Set Language. It will already be set for you.
Make sense? Better safe than sorry, right?
-George
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom