-
SQL Server中Convert函数转换日期的用法
convert函数:
作用:
- convert()函数是把日期转换为新数据类型的通用函数。
- convert() 函数可以用不同的格式显示日期/时间数据。
语法:
CONVERT(data_type(length),expression,style)
参数:data_type(length):目标数据类型(长度)
expression:需要转换的数据
style:规定日期/时间的输出格式
style规定输出的格式:
参数 | 结果 |
0/100 | 12 31 2020 8:40AM |
1 101 |
04/22/20 04/22/2020 |
2 102 |
20.12.31 2020.12.31 |
3 103 |
31/12/20 31/12/2020 |
4 104 |
31.12.20 31.12.2020 |
5 105 |
31-12-20 31-12-2020 |
6 106 |
31 12 20 31 12 2020 |
7 107 |
12 31,20 12 31,2020 |
8/108 |
09:28:24 |
9/109 |
12 31 2020 8:40:37:890AM |
10 110 |
12-31-20 12-31-2020 |
11 111 |
20/12/31 2020/12/31 |
12 112 |
201231 20201231 |
13/113 |
31 12 2020 12:31:46:780 |
14/114 | 12:33:12:250 |
20/120 |
2020-12-31 12:46:44 |
21/121 |
2020-12-31 12:48:41.437 |
22 |
12/31/20 12:50:59 PM |
23 |
2020-12-31 |
24 | 12:54:03 |
25 | 2020-12-31 13:11:21.107 |
126/127 | 2020-12-31T13:35:08.250 |
在SQLServer中的getdate()函数可以获取系统当前日期.
select Convert(Varchar(30),getdate(),0) as date; 12 31 2020 8:40AM (月 日 年 时间AM/PM)
select Convert(Varchar(30),getdate(),100) as date;
select Convert(varchar(30),getdate(),1) as date; 04/22/20 (月/日/年)
select Convert(Varchar(30),getdate(),101) as date; 04/22/2020
select Convert(Varchar(30),getdate(),2) as date; 20.12.31 (年.月.日)
select Convert(Varchar(30),getdate(),102) as date; 2020.12.31
select Convert(Varchar(30),getdate(),3) as date; 31/12/20 (日/月/年)
select Convert(Varchar(30),getdate(),103) as date; 31/12/2020
select Convert(Varchar(30),getdate(),4) as date; 31.12.20 (日.月.年)
select Convert(Varchar(30),getdate(),104) as date; 31.12.2020
select Convert(Varchar(30),getdate(),5) as date; 31-12-20 (日-月-年)
select Convert(Varchar(30),getdate(),105) as date; 31-12-2020
select Convert(Varchar(30),getdate(),6) as date; 31 12 20 (日 月 年)
select Convert(Varchar(30),getdate(),106) as date; 31 12 2020
select Convert(Varchar(30),getdate(),7) as date; 12 31,20 (月 日,年)
select Convert(Varchar(30),getdate(),107) as date; 12 31,2020
select Convert(Varchar(30),getdate(),8) as date; 09:28:24 (时:分:秒)
select Convert(Varchar(30),getdate(),108) as date;
select Convert(Varchar(30),getdate(),9) as date; 12 31 2020 8:40:37:890AM (月 日 年 时间AM/PM)
select Convert(Varchar(30),getdate(),109) as date;
select Convert(Varchar(30),getdate(),10) as date; 12-31-20 (月-日-年)
select Convert(Varchar(30),getdate(),110) as date; 12-31-2020
select Convert(Varchar(30),getdate(),11) as date; 20/12/31 (年/月/日)
select Convert(Varchar(30),getdate(),111) as date; 2020/12/31
select Convert(Varchar(30),getdate(),12) as date; 201231 (年月日)
select Convert(Varchar(30),getdate(),112) as date; 20201231
select Convert(Varchar(30),getdate(),13) as date; 31 12 2020 12:31:46:780 (日 月 年 时间)
select Convert(Varchar(30),getdate(),113) as date;
select Convert(Varchar(30),getdate(),14) as date; 12:33:12:250 (时间)
select Convert(Varchar(30),getdate(),114) as date;
select Convert(Varchar(30),getdate(),20) as date; 2020-12-31 12:46:44 (年-月-日 时:分:秒)
select Convert(Varchar(30),getdate(),120) as date;
select Convert(Varchar(30),getdate(),21) as date; 2020-12-31 12:48:41.437 (年-月-日 时间)
select Convert(Varchar(30),getdate(),121) as date;
select Convert(Varchar(30),getdate(),22) as date; 12/31/20 12:50:59 PM (月/日/年 时:分:秒 AM/PM)
select Convert(Varchar(30),getdate(),23) as date; 2020-12-31 (年-月-日)
select Convert(Varchar(30),getdate(),24) as date; 12:54:03 (时:分:秒)
select Convert(Varchar(30),getdate(),25) as date; 2020-12-31 13:11:21.107 (年-月-日 时间)
select Convert(Varchar(30),getdate(),126) as date; 2020-12-31T13:35:08.250
select Convert(Varchar(30),getdate(),127) as date;
出处:https://www.cnblogs.com/luckyfairy/p/convertfunc.html