Вы находитесь на странице: 1из 4

Sybase ASE convert 函數中的 style 日期時間樣式

Date format conversions using the style parameter


Without century (yy) With century (yyyy) Standard Output
 The default values (style 0 or 100), and style 9 or 109 return the century (yyyy).
 When converting to char or varchar from smalldatetime, styles that include seconds or milliseconds show zeros in
those positions.
 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.
 When converting date data to a character type, use style numbers 1 – 7 (101 – 107) or 10 – 12 (110 – 112) in Table
16-5 to specify the display format. The default value is 100 (mon dd yyyy hh:miAM (or PM )). If date data is
converted to a style that contains a time portion, that time portion reflects the default value of zero.
 When converting time data to a character type, use style number 8 or 9 (108 or 109) to specify the display format. The
default is 100 (mon dd yyyy hh:miAM (or PM)). If time data is converted to a style that contains a date portion, the
default date of Jan 1, 1900 appears.
- 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:ss AM (or PM)
10 110 USA mm-dd-yy
11 111 Japan yy/mm/dd
12 112 ISO yymmdd
13 113 yy/dd/mm
14 114 mm/yy/dd
14 114 hh:mi:ss:mmmAM(or PM)
15 115 dd/yy/mm
- 16 or 116 mon dd yyyy HH:mm:ss
17 117 hh:mmAM
18 118 HH:mm
19 hh:mm:ss:zzzAM
20 hh:mm:ss:zzz
21 yy/mm/dd HH:mm:ss
22 yy/mm/dd HH:mm AM (or PM)
23 yyyy-mm-ddTHH:mm:ss
SYBASE: DATETIME format conversion styles
SYNTAX : convert (datatype [(length) | (precision[, scale])][null | not null], expression [, style])

當前日期為:2010-9-24
SQL Statement Result
select convert(varchar, getdate(), 0) Sep 24 2010 5:54AM
select convert(varchar, getdate(), 1) 09/24/10
select convert(varchar, getdate(), 2) 10.09.24
select convert(varchar, getdate(), 3) 24/09/10
select convert(varchar, getdate(), 4) 24.09.10
select convert(varchar, getdate(), 5) 24-09-10
select convert(varchar, getdate(), 6) 24 Sep 10
select convert(varchar, getdate(), 7) Sep 24, 10
select convert(varchar, getdate(), 100) Sep 24 2010 5:55AM
select convert(varchar, getdate(), 101) 09/24/2010
select convert(varchar, getdate(), 102) 2010.09.24
select convert(varchar, getdate(), 103) 24/09/2010
select convert(varchar, getdate(), 104) 24.09.2010
select convert(varchar, getdate(), 105) 24-09-2010
select convert(varchar, getdate(), 106) 24 Sep 2010
select convert(varchar, getdate(), 107) Sep 24, 2010

select convert(varchar, getdate(), 8) 05:58:42


select convert(varchar, getdate(), 9) Sep 24 2010 5:58:48:640AM
select convert(varchar, getdate(), 108) 05:59:09
select convert(varchar, getdate(), 109) Sep 24 2010 5:59:27:606AM

select convert(varchar, getdate(), 10) 09-24-10


select convert(varchar, getdate(), 11) 10/09/24
select convert(varchar, getdate(), 12) 100924
select convert(varchar, getdate(), 13) 10/24/09
select convert(varchar, getdate(), 14) 09/10/24
select convert(varchar, getdate(), 15) 24/10/09
select convert(varchar, getdate(), 16) Sep 24 2010 06:02:21
select convert(varchar, getdate(), 17) 6:02AM
select convert(varchar, getdate(), 18) 06:02
select convert(varchar, getdate(), 19) 6:02:51:840AM
select convert(varchar, getdate(), 20) 06:03:07:373
select convert(varchar, getdate(), 21) 10/09/24 06:03:35
select convert(varchar, getdate(), 22) 10/09/24 6:03AM
select convert(varchar, getdate(), 23) 2010-09-24T06:03:50

select convert(varchar, getdate(), 100) Sep 24 2010 6:04AM


select convert(varchar, getdate(), 101) 09/24/2010
select convert(varchar, getdate(), 102) 2010.09.24
select convert(varchar, getdate(), 103) 24/09/2010
select convert(varchar, getdate(), 104) 24.09.2010
select convert(varchar, getdate(), 15) 24-09-2010
select convert(varchar, getdate(), 16) 24 Sep 2010
select convert(varchar, getdate(), 107) Sep 24, 2010
select convert(varchar, getdate(), 108) 06:06:13
select convert(varchar, getdate(), 109) Sep 24 2010 6:06:20:826AM
select convert(varchar, getdate(), 110) 09-24-2010
select convert(varchar, getdate(), 111) 2010/09/24
select convert(varchar, getdate(), 112) 20100924
select convert(varchar, getdate(), 113) 2010/24/09
select convert(varchar, getdate(), 114) 09/2010/24
select convert(varchar, getdate(), 115) 24/2010/09
select convert(varchar, getdate(), 116) Sep 24 2010 06:08:10
select convert(varchar, getdate(), 117) 2010/09/24 06:08:27
select convert(varchar, getdate(), 118) 2010/09/24 6:08AM

例子:
顯示格式為: YYYY-MM-DD HH:MI 的日期,如: 2011-09-24 13:48
使用 SQL 語句:
select str_replace( convert( varchar, getdate(), 102), ‘.’, ‘-’) + ‘ ‘ + convert( varchar, getdate(), 18)

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