我在Coldfusion和MS-SQL工作。
我有一个查询,它在Query Struct中提供可能的日期列表。我想将这些日期转换为相应的字符串表示。
我必须遍历查询并将每个Sql / Coldfusion日期转换为其String表示形式吗?
答案 0 :(得分:3)
您可以在服务器上执行此操作。
SELECT CAST(CONVERT(datetime, [tableName].DateField, 1) as varchar(8)) As DateString,
[tableName].DateField As DateField
FROM [tableName]
返回日期字符串WITHOUT世纪(mm / dd / yy)。
SELECT CAST(CONVERT(datetime, [tableName].DateField, 101) as varchar(10)) As DateString,
[tableName].DateField As DateField
FROM [tableName]
返回日期字符串WITH century(mm / dd / yyyy)。
有关详情,请查看here。
答案 1 :(得分:2)
-- < >: place holders that require programmer input
-- n : the length of the style format
SELECT CONVERT(<tbl>.<field>, <style code>) AS DateString FROM <tbl>
SELECT CAST(CONVERT(<tbl>.<field>, <style code>) AS varchar(<n>)) As DateString
FROM <tbl>
Style Code Format Example Style
0 or 100 mon dd yyyy hh:mmAM Mar 8 2011 9:00PM Default. Equivalent to not specifying a style code.
1 mm/dd/yy 03/23/11 USA date.
2 yy.mm.dd 11.03.23 ANSI date.
3 dd/mm/yy 23/03/11 UK / French date.
4 dd.mm.yy 23.03.11 German date.
5 dd-mm-yy 23-03-11 Italian date.
6 dd mmm yy 23 Mar 11 Abbreviated month.
7 mmm dd, yy Mar 23, 11 Abbreviated month.
8 or 108 HH:mm:ss 21:00:00 24 hour time.
9 or 109 mon dd yyyy hh:mm:ss:fffAM Mar 8 2011 9:00:00:000PM Default formatting with seconds and milliseconds appended.
10 mm-dd-yy 03-23-11 USA date with hyphen Mararators.
11 yy/mm/dd 11/03/23 Japanese date.
12 yymmdd 110323 ISO date.
13 or 113 dd mon yyyy HH:mm:ss:fff 23 Mar 2011 21:00:00:000 European default with seconds and milliseconds.
14 or 114 HH:mm:ss:fff 21:00:00:000 24 hour time with milliseconds.
20 or 120 yyyy-mm-dd HH:mm:ss 2011-03-23 21:00:00 ODBC canonical date and time.
21 or 121 yyyy-mm-dd HH:mm:ss.fff 2011-03-23 21:00:00.000 ODBC canonical date and time with milliseconds.
101 mm/dd/yyyy 03/23/2011 USA date with century.
102 yyyy.mm.dd 2011/03/23 ANSI date with century.
103 dd/mm/yyyy 23/03/2011 UK / French date with century.
104 dd.mm.yyyy 23.03.2011 German date with century.
105 dd-mm-yyyy 23-03-2011 Italian date with century.
106 dd mmm yyyy 23 Mar 2011 Abbreviated month with century.
107 mmm dd, yyyy Mar 23, 2011 Abbreviated month with century.
110 mm-dd-yyyy 03-23-2011 USA date with hyphen Mararators and century.
111 yyyy/mm/dd 2011/03/23 Japanese date with century.
112 yyyymmdd 20110323 ISO date with century.
126 yyy-mm-ddThh:mm:ss 2011-03-23T21:00:00 ISO8601, for use in XML.
来自MSDN
使用CAST然后CONVERT
SELECT
GETDATE() AS UnconvertedDateTime,
CAST(GETDATE() AS nvarchar(30)) AS UsingCast,
CONVERT(nvarchar(30), GETDATE(), 126) AS UsingConvertTo_ISO8601 ;
GO
-- Outputs:
-- UnconvertedDateTime UsingCast UsingConvertTo_ISO8601
-- ----------------------- ------------------- ------------------------------
-- 2006-04-18 09:58:04.570 Apr 18 2006 9:58AM 2006-04-18T09:58:04.570
--
-- (1 row(s) affected)
使用CONVERT然后使用CAST
SELECT
'2006-04-25T15:50:59.997' AS UnconvertedText,
CAST('2006-04-25T15:50:59.997' AS datetime) AS UsingCast,
CONVERT(datetime, '2006-04-25T15:50:59.997', 126) AS UsingConvertFrom_ISO8601 ;
GO
-- Outputs:
-- UnconvertedText UsingCast UsingConvertTo_ISO8601
-- ----------------------- ------------------- ------------------------------
-- 2006-04-25T15:50:59.997 2006-04-25 15:50:59.997 2006-04-25 15:50:59.997
--
-- (1 row(s) affected)