微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Sql中将datetime转换成字符串的方法(CONVERT)

一、回顾一下CONVERT()的语法格式:CONVERT ([ length ], [, style])
二、这里注重说明一下style的含义:style 是将DATATIME 和SMALLDATETIME 数据转换为字符串时所选用的由sql Server 系统提供的转换样式编号,不同的样式编号有不同的输出格式;一般在时间类型(datetime,smalldatetime)与字符串类型(nchar,nvarchar,char,varchar)相互转换的时候才用到.
三、下表是sql Server 系统提供的转换样式编号:

输出**
默认值
默认值 + 毫秒
默认值 + 毫秒
jri****
jri****

表中‘*'表示的含义说明: * 认值(style 0 或 100、9 或 109、13 或 113、20 或 120、21 或 121)始终返回世纪数位 (yyyy)。
** 当转换为 datetime时输入;当转换为字符数据时输出
*** 专门用于 XML。对于从 datetime或 smalldatetime 到 character 数据的转换,输出格式如表中所示。对于从 float、money 或 smallmoney 到 character 数据的转换,输出等同于 style 2。对于从 real 到 character 数据的转换,输出等同于 style 1。
**** Hijri 是具有几种变化形式的日历系统,Microsoft? sql Server? 2000 使用其中的科威特算法。

四、不带世纪数位的实例代码(注释的表示非有效的样式号):

代码如下:
SELECT CONVERT(varchar(100),GETDATE(),0) AS Style0
SELECT CONVERT(varchar(100),1) AS Style1
SELECT CONVERT(varchar(100),2) AS Style2
SELECT CONVERT(varchar(100),3) AS Style3
SELECT CONVERT(varchar(100),4) AS Style4
SELECT CONVERT(varchar(100),5) AS Style5
SELECT CONVERT(varchar(100),6) AS Style6
SELECT CONVERT(varchar(100),7) AS Style7
SELECT CONVERT(varchar(100),8) AS Style8
SELECT CONVERT(varchar(100),9) AS Style9
SELECT CONVERT(varchar(100),10) AS Style10
SELECT CONVERT(varchar(100),11) AS Style11
SELECT CONVERT(varchar(100),12) AS Style12
SELECT CONVERT(varchar(100),13) AS Style13
SELECT CONVERT(varchar(100),14) AS Style14
--SELECT CONVERT(varchar(100),15) AS Style15
--SELECT CONVERT(varchar(100),16) AS Style16
--SELECT CONVERT(varchar(100),17) AS Style17
--SELECT CONVERT(varchar(100),18) AS Style18
--SELECT CONVERT(varchar(100),19) AS Style19
SELECT CONVERT(varchar(100),20) AS Style21
SELECT CONVERT(varchar(100),21) AS Style21
SELECT CONVERT(varchar(100),22) AS Style22
SELECT CONVERT(varchar(100),23) AS Style23
SELECT CONVERT(varchar(100),24) AS Style24
SELECT CONVERT(varchar(100),25) AS Style25
--SELECT CONVERT(varchar(100),26) AS Style26
--SELECT CONVERT(varchar(100),27) AS Style27
--SELECT CONVERT(varchar(100),28) AS Style28
--SELECT CONVERT(varchar(100),29) AS Style29
--SELECT CONVERT(varchar(100),30) AS Style30
--SELECT CONVERT(varchar(100),31) AS Style31

运行结果:
<asql中将datetime转换成字符串的方法 src="https:https://files.jb51.cc/file_images/article/201404/201404202320512.png">
<asql中将datetime转换成字符串的方法 src="https:https://files.jb51.cc/file_images/article/201404/201404202320513.png">
<asql中将datetime转换成字符串的方法 src="https:https://files.jb51.cc/file_images/article/201404/201404202320514.png">

五、带世纪数位的实例代码(注释的表示非有效的样式号):

代码如下:
SELECT CONVERT(varchar(100),100) AS Style100
SELECT CONVERT(varchar(100),101) AS Style101
SELECT CONVERT(varchar(100),102) AS Style102
SELECT CONVERT(varchar(100),103) AS Style103
SELECT CONVERT(varchar(100),104) AS Style104
SELECT CONVERT(varchar(100),105) AS Style105
SELECT CONVERT(varchar(100),106) AS Style106
SELECT CONVERT(varchar(100),107) AS Style107
SELECT CONVERT(varchar(100),108) AS Style108
SELECT CONVERT(varchar(100),109) AS Style109
SELECT CONVERT(varchar(100),110) AS Style110
SELECT CONVERT(varchar(100),111) AS Style111
SELECT CONVERT(varchar(100),112) AS Style112
SELECT CONVERT(varchar(100),113) AS Style113
SELECT CONVERT(varchar(100),114) AS Style114
--SELECT CONVERT(varchar(100),115) AS Style115
--SELECT CONVERT(varchar(100),116) AS Style116
--SELECT CONVERT(varchar(100),117) AS Style117
--SELECT CONVERT(varchar(100),118) AS Style118
--SELECT CONVERT(varchar(100),119) AS Style119
SELECT CONVERT(varchar(100),120) AS Style121
SELECT CONVERT(varchar(100),121) AS Style121
--SELECT CONVERT(varchar(100),122) AS Style122
--SELECT CONVERT(varchar(100),123) AS Style123
--SELECT CONVERT(varchar(100),124) AS Style124
--SELECT CONVERT(varchar(100),125) AS Style125
SELECT CONVERT(varchar(100),126) AS Style126
SELECT CONVERT(varchar(100),127) AS Style127
--SELECT CONVERT(varchar(100),128) AS Style128
--SELECT CONVERT(varchar(100),129) AS Style129
SELECT CONVERT(varchar(100),130) AS Style130
SELECT CONVERT(varchar(100),131) AS Style131

运行结果:
<asql中将datetime转换成字符串的方法 src="https:https://files.jb51.cc/file_images/article/201404/201404202320515.png">
<asql中将datetime转换成字符串的方法 src="https:https://files.jb51.cc/file_images/article/201404/201404202320516.png">
sql将datetime转化为字符串并截取字符串
代码如下:
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 '转换格式并截取后的时间'from dbo.tb_sell_resources,dbo.tb_sell_selldetails where sr_id = ss_rsid and ss_qdstate <> 1 order by ss_updatetime
sql 中字符串截取函数: SUBSTRING(name,start,end)
name: 字符串格式的 字段名
start: 规定开始位置(起始值是 1)
end:截取字符串结束的位置
sql 中Datetime格式转换为字符串格式: 2000-01-01 01:01:01(Datetime) CONVERT(CHAR(19),CURRENT_TIMESTAMP,120) CURRENT_TIMESTAMP: 当前时间(此处可以写Datetime格式的字段名,例如ss_updatetime)
其余的参数(CHAR(19),120等)不用修改
使用之后 2000-01-01 01:01:01(Datetime)变为 2000-01-01 01:01:01(字符串格式)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐