Search This Blog

Saturday, June 29, 2013

SYBASE: DATETIME format conversion styles

SYNTAX :
convert (datatype [(length) | (precision[, scale])]
        [null | not null], expression [, style])

Without century (yy)
With century (yyyy)
Standard
Output
Key “mon” indicates a month spelled out, “mm” the month number or minutes. “HH ”indicates a 24-hour clock value, “hh” a 12-hour clock value. The last row, 23, includes a literal “T” to separate the date and time portions of the format.
-
0 or 100
Default
mon dd yyyy hh:mm AM (or PM)
1
101
USA
mm/dd/yy
2
2
SQL standard
yy.mm.dd
3
103
English/French
dd/mm/yy
4
104
German
dd.mm.yy
5
105
dd-mm-yy
6
106
dd mon yy
7
107
mon dd, yy
8
108
HH:mm:ss
-
9 or 109
Default + milliseconds
mon dd yyyy hh:mm:sss AM (or PM)
10
110
USA
mm-dd-yy
11
111
Japan
yy/mm/dd
12
112
ISO
yy/mm/dd
13
113
yy/mm/dd
14
114
yy/mm/dd
14
114
hh:mi:ss:mmmAM(or PM)
15
115
dd/yy/mm
16
116
mon dd yy HH:mm:ss
17
117
hh:mmAM
18
118
HH:mm
19
119
hh:mm:ss:zzzAM
20
120
hh:mm:ss:zzz
21
121
yy/mm/dd
22
122
yy/mm/dd
23
123
yyyy-mm-ddTHH:mm:ss

Some Examples:
SELECT CONVERT(CHAR(50), DATETIME('2009-11-03 11:10:42.033189'), 136) FROM iq_dummy returns 11:10:42.033189AM
SELECT CONVERT(CHAR(50), NOW(), 137) FROM iq_dummy returns 14:54:48.794122
The following statements illustrate the use of the format style 365, which converts data of type DATE and DATETIME to and from either string or integer type data:
CREATE TABLE tab
   (date_col DATE, int_col INT, char7_col CHAR(7));
INSERT INTO tab (date_col, int_col, char7_col)
   VALUES (‘Dec 17, 2004’, 2004352, ‘2004352’);
SELECT CONVERT(VARCHAR(8), tab.date_col, 365) FROM tab; returns ‘2004352’
SELECT CONVERT(INT, tab.date_col, 365) from tab; returns 2004352
SELECT CONVERT(DATE, tab.int_col, 365) FROM TAB; returns 2004-12-17
SELECT CONVERT(DATE, tab.char7_col, 365) FROM tab; returns 2004-12-17

No comments:

Post a Comment