简体   繁体   中英

Converting SQL Date to String

I am working in Coldfusion and MS-SQL.

I have a query that delivers a list of possible dates in a Query Struct. I want to convert these dates to their corresponding String representations.

Must I loop over the query and convert each Sql/Coldfusion date to its String representation?

You could do it on the server.

SELECT CAST(CONVERT(datetime, [tableName].DateField, 1) as varchar(8)) As DateString,
[tableName].DateField As DateField
FROM [tableName]

This returns a date string WITHOUT century (mm/dd/yy).

SELECT CAST(CONVERT(datetime, [tableName].DateField, 101) as varchar(10)) As DateString,
[tableName].DateField As DateField
FROM [tableName]

This returns a date string WITH century (mm/dd/yyyy).

For more information, check here .

-- < >: 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>

Date Styles

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.

From MSDN

Using CAST then 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) 

Using CONVERT then 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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM