|
Execute the following Microsoft SQL Server T-SQL example script to demonstrate sql datetime to text conversion.
The following
conversion options are available for sql datetime format without century (YY format). Note that some conversions do include century. They are listed for completeness of the 0 - 25 option range. Adding 100 to the style number will result in the century format - YYYY or CCYY.
-- SQL convert datetime to text
-- SQL convert datetime to varchar
SELECT TextDate = convert(CHAR,getdate(), 0) --mon dd yyyy hh:mmAM (or PM)
-- Feb 15 2019 5:41PM
SELECT TextDate = convert(CHAR,getdate(), 1) --mm/dd/yy
-- 02/15/19
SELECT TextDate = convert(CHAR,getdate(), 2) --yy.mm.dd
SELECT TextDate = convert(CHAR,getdate(), 3) --dd/mm/yy
SELECT TextDate = convert(CHAR,getdate(), 4) --dd.mm.yy
SELECT TextDate = convert(CHAR,getdate(), 5) --dd-mm-yy
SELECT TextDate = convert(CHAR,getdate(), 6) --dd mon yy
SELECT TextDate = convert(CHAR,getdate(), 7) --mon dd, yy
SELECT TextDate = convert(CHAR,getdate(), 8) --hh:mm:ss
SELECT TextDate = convert(CHAR,getdate(), 9) --mon dd yy hh:mm:ss:mmmAM (or PM)
SELECT TextDate = convert(CHAR,getdate(),10) --mm-dd-yy
SELECT TextDate = convert(CHAR,getdate(),11) --yy/mm/dd
SELECT TextDate = convert(CHAR,getdate(),12) --yymmdd
SELECT TextDate = convert(CHAR,getdate(),13) --dd mon yyyy hh:mm:ss:mmm
SELECT TextDate = convert(CHAR,getdate(),14) --hh:mm:ss:mmm(24h)
SELECT TextDate = convert(CHAR,getdate(),20) --yyyy-mm-dd hh:mm:ss(24h)
SELECT TextDate = convert(CHAR,getdate(),21) --yyyy-mm-dd hh:mm:ss.mmm
SELECT TextDate = convert(CHAR,getdate(),22) --mm/dd/yy hh:mm:ss AM (or PM)
SELECT TextDate = convert(CHAR,getdate(),23) --yyyy-mm-dd
SELECT TextDate = convert(CHAR,getdate(),24) --hh:mm:ss
SELECT TextDate = convert(CHAR,getdate(),25) --yyyy-mm-dd hh:mm:ss.mmm
|