Home > Database > Mysql Tutorial > SQL Server中CONVERT()日期转换函数的用法

SQL Server中CONVERT()日期转换函数的用法

WBOY
Release: 2016-06-07 17:49:29
Original
1938 people have browsed it

首先我们在官网会看到关于convert函数的介绍,是讲CONVERT() 函数是把日期转换为新数据类型的通用函数和CONVERT() 函数可以用不同的格式显示日期/时间数据。

先来看语汇法

 代码如下 复制代码

CONVERT(data_type(length),data_to_be_converted,style)

例1

 代码如下 复制代码

SELECT CONVERT(varchar(30),getdate(),101) now

结果为:

now
---------------------------------------
|03/8/2013

例2

 代码如下 复制代码

1> select CONVERT(varchar(8),getdate(),112)  
2> go
 
20130308
(1 row affected)
1> select CONVERT(varchar(10),getdate(),105)      
2> go
 
08-03-2013
(1 row affected)
1> select getdate()
2> go
 
2013-03-08 10:30:30
(1 row affected)
1>

获取某天所有的数据

 代码如下 复制代码

1> select * from s_sign_log
2> go
id      user_id room_id create_time
1       297211672       8586    2013-03-07 16:05:52
2       297211672       8588    2013-03-07 16:17:18
3       297211671       8588    2013-03-07 16:25:14
4       297211671       9983    2013-03-07 18:21:55
5       297211671       9983    2013-03-08 10:33:17
(5 rows affected)

调出昨天的数据

 代码如下 复制代码

1> select * from s_sign_log where CONVERT(varchar(8),create_time,112) = CONVERT(varchar(8),dateadd(dd,-1,getdate()),112)
2> go
id      user_id room_id create_time
1       297211672       8586    2013-03-07 16:05:52
2       297211672       8588    2013-03-07 16:17:18
3       297211671       8588    2013-03-07 16:25:14
4       297211671       9983    2013-03-07 18:21:55
(4 rows affected)

语句及查询结果:

 代码如下 复制代码
SELECT CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AM
SELECT CONVERT(varchar(100), GETDATE(), 1): 05/16/06
SELECT CONVERT(varchar(100), GETDATE(), 2): 06.05.16
SELECT CONVERT(varchar(100), GETDATE(), 3): 16/05/06
SELECT CONVERT(varchar(100), GETDATE(), 4): 16.05.06
SELECT CONVERT(varchar(100), GETDATE(), 5): 16-05-06
SELECT CONVERT(varchar(100), GETDATE(), 6): 16 05 06
SELECT CONVERT(varchar(100), GETDATE(), 7): 05 16, 06
SELECT CONVERT(varchar(100), GETDATE(), 8): 10:57:46
SELECT CONVERT(varchar(100), GETDATE(), 9): 05 16 2006 10:57:46:827AM
SELECT CONVERT(varchar(100), GETDATE(), 10): 05-16-06
SELECT CONVERT(varchar(100), GETDATE(), 11): 06/05/16
SELECT CONVERT(varchar(100), GETDATE(), 12): 060516
SELECT CONVERT(varchar(100), GETDATE(), 13): 16 05 2006 10:57:46:937
SELECT CONVERT(varchar(100), GETDATE(), 14): 10:57:46:967
SELECT CONVERT(varchar(100), GETDATE(), 20): 2006-05-16 10:57:47
SELECT CONVERT(varchar(100), GETDATE(), 21): 2006-05-16 10:57:47.157
SELECT CONVERT(varchar(100), GETDATE(), 22): 05/16/06 10:57:47 AM
SELECT CONVERT(varchar(100), GETDATE(), 23): 2006-05-16
SELECT CONVERT(varchar(100), GETDATE(), 24): 10:57:47
SELECT CONVERT(varchar(100), GETDATE(), 25): 2006-05-16 10:57:47.250
SELECT CONVERT(varchar(100), GETDATE(), 100): 05 16 2006 10:57AM
SELECT CONVERT(varchar(100), GETDATE(), 101): 05/16/2006
SELECT CONVERT(varchar(100), GETDATE(), 102): 2006.05.16
SELECT CONVERT(varchar(100), GETDATE(), 103): 16/05/2006
SELECT CONVERT(varchar(100), GETDATE(), 104): 16.05.2006
SELECT CONVERT(varchar(100), GETDATE(), 105): 16-05-2006
SELECT CONVERT(varchar(100), GETDATE(), 106): 16 05 2006
SELECT CONVERT(varchar(100), GETDATE(), 107): 05 16, 2006
SELECT CONVERT(varchar(100), GETDATE(), 108): 10:57:49
SELECT CONVERT(varchar(100), GETDATE(), 109): 05 16 2006 10:57:49:437AM
SELECT CONVERT(varchar(100), GETDATE(), 110): 05-16-2006
SELECT CONVERT(varchar(100), GETDATE(), 111): 2006/05/16
SELECT CONVERT(varchar(100), GETDATE(), 112): 20060516
SELECT CONVERT(varchar(100), GETDATE(), 113): 16 05 2006 10:57:49:513
SELECT CONVERT(varchar(100), GETDATE(), 114): 10:57:49:547
SELECT CONVERT(varchar(100), GETDATE(), 120): 2006-05-16 10:57:49
SELECT CONVERT(varchar(100), GETDATE(), 121): 2006
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template