A silly question maybe but I wanted clarification. I’ve created a script that has a date parameter like so:
DECLARE @dateparam as datetime
SET @dateparam = '01-01-2013 00:00:00'
This looks like it is working when I test it even if the date string is not in “correct” format yyyy-MM-dd hh:mm:ss. I changed my computer regional settings to English and the script still did what it was supposed to do.
Is this because of SQL Server 2008 R2 that I have in my computer that it knows how to convert the date or can I ran into trouble with using a dateformat like I have used?
Converting
01-01-2013won’t expose issues such as which01is the month, and which is the day.It’s not a safe format.
The safe formats (for converting to
datetime, rather than todatetime2) are:Stick to those and only those. (The examples all represent the 1st December 2012)
Or, better yet, don’t treat dates as strings at all, if you can avoid it. If you’re, for example, calling SQL Server from .NET code, keep that dates as
DateTimes in your code, and let ADO.NET and SQL Server deal with any required translations to make them becomedatetimes – without translating them to and from strings.