即使使用古老的MSSQL Server 7.0,这里的代码(由这个链接提供)也允许我获得我当时正在寻找的任何日期格式:
PRINT '1) Date/time in format MON DD YYYY HH:MI AM (OR PM): ' + CONVERT(CHAR(19),GETDATE())PRINT '2) Date/time in format MM-DD-YY: ' + CONVERT(CHAR(8),GETDATE(),10)PRINT '3) Date/time in format MM-DD-YYYY: ' + CONVERT(CHAR(10),GETDATE(),110)PRINT '4) Date/time in format DD MON YYYY: ' + CONVERT(CHAR(11),GETDATE(),106)PRINT '5) Date/time in format DD MON YY: ' + CONVERT(CHAR(9),GETDATE(),6)PRINT '6) Date/time in format DD MON YYYY HH:MM:SS:MMM(24H): ' + CONVERT(CHAR(24),GETDATE(),113)
它产生了这样的输出:
1) Date/time in format MON DD YYYY HH:MI AM (OR PM): Feb 27 2015 1:14PM2) Date/time in format MM-DD-YY: 02-27-153) Date/time in format MM-DD-YYYY: 02-27-20154) Date/time in format DD MON YYYY: 27 Feb 20155) Date/time in format DD MON YY: 27 Feb 156) Date/time in format DD MON YYYY HH:MM:SS:MMM(24H): 27 Feb 2015 13:14:46:630
2009-06-15T13:45:30 -> Monday, June 15, 2009 (en-US)2009-06-15T13:45:30 -> 15 июня 2009 г. (ru-RU)2009-06-15T13:45:30 -> Montag, 15. Juni 2009 (de-DE)
查询中的更多示例。
DECLARE @d DATETIME = '10/01/2011';SELECT FORMAT ( @d, 'd', 'en-US' ) AS 'US English Result',FORMAT ( @d, 'd', 'en-gb' ) AS 'Great Britain English Result',FORMAT ( @d, 'd', 'de-de' ) AS 'German Result',FORMAT ( @d, 'd', 'zh-cn' ) AS 'Simplified Chinese (PRC) Result';
SELECT FORMAT ( @d, 'D', 'en-US' ) AS 'US English Result',FORMAT ( @d, 'D', 'en-gb' ) AS 'Great Britain English Result',FORMAT ( @d, 'D', 'de-de' ) AS 'German Result',FORMAT ( @d, 'D', 'zh-cn' ) AS 'Chinese (Simplified PRC) Result';
US English Result Great Britain English Result German Result Simplified Chinese (PRC) Result---------------- ----------------------------- ------------- -------------------------------------10/1/2011 01/10/2011 01.10.2011 2011/10/1
US English Result Great Britain English Result German Result Chinese (Simplified PRC) Result---------------------------- ----------------------------- ----------------------------- ---------------------------------------Saturday, October 01, 2011 01 October 2011 Samstag, 1. Oktober 2011 2011年10月1日
SELECT CAST(GETDATE() AS DATE) AS 'Date1'SELECT Date2 = CONVERT(DATE, GETDATE())SELECT CONVERT(DATE, GETDATE()) AS 'Date3'SELECT CONVERT(CHAR(10), GETDATE(), 121) AS 'Date4'SELECT CONVERT(CHAR(10), GETDATE(), 126) AS 'Date5'SELECT CONVERT(CHAR(10), GETDATE(), 127) AS 'Date6'