The YYYY-MM-DD format is unambiguous, which means that SQL Server will not confuse the month and day when converting a string value to DATETIME . (I have never experienced an implicit conversion problem using this format using a four-digit year.)
The βsafestβ (and most convenient) way to store date values ββin SQL Server is to use the DATETIME data type.
Use the CONVERT function to explicitly specify input and output formats when converting between DATETIME and strings.
SQL Server 2005 documentation for CONVERT style values:
http://msdn.microsoft.com/en-us/library/ms187928(SQL.90).aspx
To convert a string representation to a DATETIME data type:
select CONVERT(datetime, '2009-06-03', 20)
The first argument is the data type to convert, the second argument is the converted expression, the third argument is the style.
(style 20 - Canonical ODBC format = 'YYYY-MM-DD HH:MI:SS' (24-hour mode)
[Followup]
To convert a DATETIME expression (e.g. getdate () to VARCHAR in the format 'YYYY-MM-DD' :
select CONVERT(varchar(10), getdate(), 20)
Note that specifying varchar (10) gives you only the first 10 characters of the etnire format 'YYYY-MM-DD HH:MM:SS' .
[/ Followup]
As for the default formats, this will be a study. We avoid problems caused by default formats by defining formats.
spencer7593
source share