当前位置:文档之家› SQL中CONVERT转化日期函数的使用方法

SQL中CONVERT转化日期函数的使用方法

SQLserver中用convert函数转换日期格式的方法
>SQL中CONVERT函数最常用的是使用convert转化长日期为短日期
如果只要取yyyy-mm-dd格式时间, 就可以用 convert(nvarchar(10),field,120)
120 是格式代码, nvarchar(10) 是指取出前10位字符.
SELECT CONVERT(nvarchar(10), getdate(), 120)
SELECT CONVERT(varchar(10), getdate(), 120)
SELECT CONVERT(char(10), getdate(), 120)
=======================================================
使用 CONVERT:
CONVERT (data_type[(length)], expression [, style])
select CONVERT(varchar, getdate(), 120 )
2004-09-12 11:06:08
select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),’-',”),’ ‘,”),’:',”)
20040912110608
select CONVERT(varchar(12) , getdate(), 111 )
2004/09/12
select CONVERT(varchar(12) , getdate(), 112 )
20040912
select CONVERT(varchar(12) , getdate(), 102 )
2004.09.12
select CONVERT(varchar(12) , getdate(), 101 )
09/12/2004
select CONVERT(varchar(12) , getdate(), 103 )
12/09/2004
select CONVERT(varchar(12) , getdate(), 104 )
12.09.2004
select CONVERT(varchar(12) , getdate(), 105 )
12-09-2004
select CONVERT(varchar(12) , getdate(), 106 )
12 09 2004
select CONVERT(varchar(12) , getdate(), 107 )
09 12, 2004
select CONVERT(varchar(12) , getdate(), 108 )
11:06:08
select CONVERT(varchar(12) , getdate(), 109 )
09 12 2004 1
select CONVERT(varchar(12) , getdate(), 110 )
09-12-2004
select CONVERT(varchar(12) , getdate(), 113 )
12 09 2004 1
select CONVERT(varchar(12) , getdate(), 114 )
11:06:08.177
SQL Server中文版的默认的日期字段datetime格式是yyyy-mm-dd Thh:mm:ss.mmm 例如:
select getdate()
整理了一下SQL Server里面可能经常会用到的日期格式转换方法:
举例如下:
select CONVERT(varchar, getdate(), 120 )
2004-09-12 11:06:08
select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','') 20040912110608
select CONVERT(varchar(12) , getdate(), 111 )
2004/09/12
select CONVERT(varchar(12) , getdate(), 112 )
20040912
select CONVERT(varchar(12) , getdate(), 102 )
2004.09.12
select CONVERT(varchar(12) , getdate(), 101 )
09/12/2004
select CONVERT(varchar(12) , getdate(), 103 )
12/09/2004
select CONVERT(varchar(12) , getdate(), 104 ) 12.09.2004
select CONVERT(varchar(12) , getdate(), 105 ) 12-09-2004
select CONVERT(varchar(12) , getdate(), 106 ) 12 09 2004
select CONVERT(varchar(12) , getdate(), 107 ) 09 12, 2004
select CONVERT(varchar(12) , getdate(), 108 ) 11:06:08
select CONVERT(varchar(12) , getdate(), 109 ) 09 12 2004 1
select CONVERT(varchar(12) , getdate(), 110 ) 09-12-2004
select CONVERT(varchar(12) , getdate(), 113 ) 12 09 2004 1
select CONVERT(varchar(12) , getdate(), 114 ) 11:06:08.177
select getdate()
结果:2003-12-28 16:52:00.107
select convert(char(8),getdate(),112)
结果:20031228
select convert(char(8),getdate(),108)
结果:16:52:00
select convert(char(8),getdate(),112)
指日期格式
规则如下:
1 101 美国mm/dd/yyyy
2 102 ANSI yy.mm.dd
3 103 英国/法国dd/mm/yy
4 104 德国dd.mm.yy
5 105 意大利dd-mm-yy
6 106 - dd mon yy
7 107 - mon dd, yy
8 108 - hh:mm:ss
- 9 或109 (*) 默认值+ 毫秒mon dd yyyy hh:mi:ss:mmmAM(或PM)
10 110 美国mm-dd-yy
11 111 日本yy/mm/dd
12 112 ISO yymmdd
- 13 或113 (*) 欧洲默认值+ 毫秒dd mon yyyy hh:mm:ss:mmm(24h)
14 114 - hh:mi:ss:mmm(24h)
- 20 或120 (*) ODBC 规范yyyy-mm-dd hh:mm:ss[.fff]
- 21 或121 (*) ODBC 规范(带毫秒)yyyy-mm-dd hh:mm:ss[.fff]
- 126(***) ISO8601 yyyy-mm-dd Thh:mm:ss:mmm(不含空格)
- 130* 科威特dd mon yyyy hh:mi:ss:mmmAM
- 131* 科威特dd/mm/yy hh:mi:ss:mmmAM
关于Emaker中字段的格式转换和字段间的运算代码可以加到属性里的“格式转换(读出)”和“格式转换(写入)”,table字段设定里的“字段”位置也可以灵活加函数。

比如:'AF'+ID
或者ID+'/'+PWD ,convert(varchar(50),F1) ,
convert(int,%)-19110000 (读出)
convert(char(8),convert(int,%)+19110000) (写入)
实现行的合计运算等等。

加入:%系统变量%,则是调用在Emaker 系统中设定的系统变量。

您可能感兴趣的文章:
∙SQL Server 日期函数CAST 和CONVERT 以及在业务中的使用介绍
∙SQL Server 日期相关资料详细介绍
∙sqlserver获取当前日期的最大时间值
∙sqlserver中如何查询出连续日期记录的代码
∙sql 查询本年、本月、本日记录的语句,附SQL日期函数
∙Sqlserver 常用日期时间函数
∙根据日期知道当天是星期几的手动计算方法
∙按日期选择最晚日期与最早日期对应的数的差值
∙MSSQL 计算两个日期相差的工作天数的语句
∙SQL Server自动生成日期加数字的序列号
∙SQL 返回期间内的所有日期
∙数据库日期加减处理
∙sql语句中如何将datetime格式的日期转换为yy-mm-dd格式
∙在SQL中使用convert函数进行日期的查询的代码
∙SQL Server日期计算
∙SQL Server各种日期计算方法(收藏)
∙SqlServer中的日期与时间函数。

相关主题