Mega Code Archive

 
Categories / MSSQL Tutorial / Data Convert Functions
 

CONVERT function syntax

CONVERT(data_ type(<length>), expression, <style>) The length option is only used on the following data types: nchar, char, varchar, nvarchar, varbinary, and binary. SQL Server interprets two digit years as follows:  if the year is less than 49 then the year is considered to be 20XX  while anything 50 and over is considered 19XX. 12> SELECT 13>    CAST('11/11/72' as smalldatetime) AS '11/11/72', 14>    CAST('6/5/40' as smalldatetime) as '6/5/40' 15> GO 11/11/72             6/5/40 -------------------- --------------------  1972-11-11 00:00:00  2040-06-05 00:00:00 (1 rows affected) 1> SELECT 2>    CONVERT(varchar, getdate(), 1), 3>    CONVERT(varchar, getdate(), 2), 4>    CONVERT(varchar, getdate(), 3), 5>    CONVERT(varchar, getdate(), 4), 6>    CONVERT(varchar, getdate(), 5), 7>    CONVERT(varchar, getdate(), 6) 8> GO ------------------------------ ------------------------------ ------------------------------ --------------------------- --- ------------------------------ ------------------------------ 10/21/07                       07.10.21                       21/10/07                       21.10.07     21-10-07                       21 Oct 07 (1 rows affected)