一、回顧一下CONVERT()的語法格式:CONVERT (<data_ type>[ length ], <expression> [, style])
二、這里注重說明一下style的含義:style 是將DATATIME 和SMALLDATETIME 數(shù)據(jù)轉(zhuǎn)換為字符串時所選用的由SQL Server 系統(tǒng)提供的轉(zhuǎn)換樣式編號,不同的樣式編號有不同的輸出格式;一般在時間類型(datetime,smalldatetime)與字符串類型(nchar,nvarchar,char,varchar)相互轉(zhuǎn)換的時候才用到.
三、下表是SQL Server 系統(tǒng)提供的轉(zhuǎn)換樣式編號:
表中‘*'表示的含義說明: * 默認(rèn)值(style 0 或 100、9 或 109、13 或 113、20 或 120、21 或 121)始終返回世紀(jì)數(shù)位 (yyyy)。
** 當(dāng)轉(zhuǎn)換為 datetime時輸入;當(dāng)轉(zhuǎn)換為字符數(shù)據(jù)時輸出。
*** 專門用于 XML。對于從 datetime或 smalldatetime 到 character 數(shù)據(jù)的轉(zhuǎn)換,輸出格式如表中所示。對于從 float、money 或 smallmoney 到 character 數(shù)據(jù)的轉(zhuǎn)換,輸出等同于 style 2。對于從 real 到 character 數(shù)據(jù)的轉(zhuǎn)換,輸出等同于 style 1。
**** Hijri 是具有幾種變化形式的日歷系統(tǒng),Microsoft? SQL Server? 2000 使用其中的科威特算法。
四、不帶世紀(jì)數(shù)位的實例代碼(注釋的表示非有效的樣式號):
代碼如下:
SELECT CONVERT(varchar(100), GETDATE(), 0) AS Style0
SELECT CONVERT(varchar(100), GETDATE(), 1) AS Style1
SELECT CONVERT(varchar(100), GETDATE(), 2) AS Style2
SELECT CONVERT(varchar(100), GETDATE(), 3) AS Style3
SELECT CONVERT(varchar(100), GETDATE(), 4) AS Style4
SELECT CONVERT(varchar(100), GETDATE(), 5) AS Style5
SELECT CONVERT(varchar(100), GETDATE(), 6) AS Style6
SELECT CONVERT(varchar(100), GETDATE(), 7) AS Style7
SELECT CONVERT(varchar(100), GETDATE(), 8) AS Style8
SELECT CONVERT(varchar(100), GETDATE(), 9) AS Style9
SELECT CONVERT(varchar(100), GETDATE(), 10) AS Style10
SELECT CONVERT(varchar(100), GETDATE(), 11) AS Style11
SELECT CONVERT(varchar(100), GETDATE(), 12) AS Style12
SELECT CONVERT(varchar(100), GETDATE(), 13) AS Style13
SELECT CONVERT(varchar(100), GETDATE(), 14) AS Style14
--SELECT CONVERT(varchar(100), GETDATE(), 15) AS Style15
--SELECT CONVERT(varchar(100), GETDATE(), 16) AS Style16
--SELECT CONVERT(varchar(100), GETDATE(), 17) AS Style17
--SELECT CONVERT(varchar(100), GETDATE(), 18) AS Style18
--SELECT CONVERT(varchar(100), GETDATE(), 19) AS Style19
SELECT CONVERT(varchar(100), GETDATE(), 20) AS Style21
SELECT CONVERT(varchar(100), GETDATE(), 21) AS Style21
SELECT CONVERT(varchar(100), GETDATE(), 22) AS Style22
SELECT CONVERT(varchar(100), GETDATE(), 23) AS Style23
SELECT CONVERT(varchar(100), GETDATE(), 24) AS Style24
SELECT CONVERT(varchar(100), GETDATE(), 25) AS Style25
--SELECT CONVERT(varchar(100), GETDATE(), 26) AS Style26
--SELECT CONVERT(varchar(100), GETDATE(), 27) AS Style27
--SELECT CONVERT(varchar(100), GETDATE(), 28) AS Style28
--SELECT CONVERT(varchar(100), GETDATE(), 29) AS Style29
--SELECT CONVERT(varchar(100), GETDATE(), 30) AS Style30
--SELECT CONVERT(varchar(100), GETDATE(), 31) AS Style31
運行結(jié)果:
五、帶世紀(jì)數(shù)位的實例代碼(注釋的表示非有效的樣式號):
代碼如下:
SELECT CONVERT(varchar(100), GETDATE(), 100) AS Style100
SELECT CONVERT(varchar(100), GETDATE(), 101) AS Style101
SELECT CONVERT(varchar(100), GETDATE(), 102) AS Style102
SELECT CONVERT(varchar(100), GETDATE(), 103) AS Style103
SELECT CONVERT(varchar(100), GETDATE(), 104) AS Style104
SELECT CONVERT(varchar(100), GETDATE(), 105) AS Style105
SELECT CONVERT(varchar(100), GETDATE(), 106) AS Style106
SELECT CONVERT(varchar(100), GETDATE(), 107) AS Style107
SELECT CONVERT(varchar(100), GETDATE(), 108) AS Style108
SELECT CONVERT(varchar(100), GETDATE(), 109) AS Style109
SELECT CONVERT(varchar(100), GETDATE(), 110) AS Style110
SELECT CONVERT(varchar(100), GETDATE(), 111) AS Style111
SELECT CONVERT(varchar(100), GETDATE(), 112) AS Style112
SELECT CONVERT(varchar(100), GETDATE(), 113) AS Style113
SELECT CONVERT(varchar(100), GETDATE(), 114) AS Style114
--SELECT CONVERT(varchar(100), GETDATE(), 115) AS Style115
--SELECT CONVERT(varchar(100), GETDATE(), 116) AS Style116
--SELECT CONVERT(varchar(100), GETDATE(), 117) AS Style117
--SELECT CONVERT(varchar(100), GETDATE(), 118) AS Style118
--SELECT CONVERT(varchar(100), GETDATE(), 119) AS Style119
SELECT CONVERT(varchar(100), GETDATE(), 120) AS Style121
SELECT CONVERT(varchar(100), GETDATE(), 121) AS Style121
--SELECT CONVERT(varchar(100), GETDATE(), 122) AS Style122
--SELECT CONVERT(varchar(100), GETDATE(), 123) AS Style123
--SELECT CONVERT(varchar(100), GETDATE(), 124) AS Style124
--SELECT CONVERT(varchar(100), GETDATE(), 125) AS Style125
SELECT CONVERT(varchar(100), GETDATE(), 126) AS Style126
SELECT CONVERT(varchar(100), GETDATE(), 127) AS Style127
--SELECT CONVERT(varchar(100), GETDATE(), 128) AS Style128
--SELECT CONVERT(varchar(100), GETDATE(), 129) AS Style129
SELECT CONVERT(varchar(100), GETDATE(), 130) AS Style130
SELECT CONVERT(varchar(100), GETDATE(), 131) AS Style131
運行結(jié)果:
SQL將datetime轉(zhuǎn)化為字符串并截取字符串
代碼如下:
select sr_child as '孩子姓名', sr_parents as '家長姓名' ,ss_updatetime as '分配時間', left(ss_updatetime,CHARINDEX(' ',ss_updatetime)-1),SUBSTRING(CONVERT(CHAR(19), ss_updatetime, 120),1,10)as '轉(zhuǎn)換格式并截取后的時間'from dbo.tb_sell_resources,dbo.tb_sell_selldetails where sr_id = ss_rsid and ss_qdstate <> 1 order by ss_updatetime
sql 中字符串截取函數(shù): SUBSTRING(name,start,end)
name: 字符串格式的 字段名
start: 規(guī)定開始位置(起始值是 1)
end:截取字符串結(jié)束的位置
sql 中Datetime格式轉(zhuǎn)換為字符串格式: 2000-01-01 01:01:01(Datetime) CONVERT(CHAR(19), CURRENT_TIMESTAMP, 120)
CURRENT_TIMESTAMP: 當(dāng)前時間(此處可以寫Datetime格式的字段名,例如ss_updatetime)
其余的參數(shù)(CHAR(19), 120等)不用修改
使用之后 2000-01-01 01:01:01(Datetime)變?yōu)?2000-01-01 01:01:01(字符串格式)
更多信息請查看IT技術(shù)專欄