SQL server date format conversion

Date format conversion

字符串转日期时间:
convert(datetime,‘2017-12-12 00:00:01’, 20)
日期时间转字符串:
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-05-16 10:57:49.700
Select CONVERT(varchar(100), GETDATE(), 126): 2006-05-16T10:57:49.827
Select CONVERT(varchar(100), GETDATE(), 130): 18 ??? ??? 1427 10:57:49:907AM

sql example:

SELECT A.a,A.b, CONVERT(varchar(100), A.日期, 20) AS A_DATETIME--显示的格式 2006-05-16 10:57:47
FROM table1 AS A
WHERE CONVERT(varchar(100), A.日期, 126) ='2016-03-04'--日期格式转为字符串,查询可以用全等(=)

Sql Server date and time functions:

//1. 当前系统日期、时间 
SELECT getdate() 
//2. dateadd  在向指定日期加上一段时间的基础上,返回新的 datetime 值
SELECT dateadd(day,2,'2020-10-15')  --返回:2020-10-17 00:00:00.000
//3. datediff 返回跨两个指定日期的日期和时间边界数
SELECT datediff(day,'2020-09-01','2020-09-18')   --返回:17
//4. datepart 返回代表指定日期的指定日期部分的整数
SELECT DATEPART(month, '2004-10-15')  --返回 10
//5. datename 返回代表指定日期的指定日期部分的字符串
SELECT datename(weekday, '2004-10-15')  --返回:星期五



  1. day(), month(), year()-can be compared with datepart
    select current date=convert(varchar(10),getdate(),120), current time=convert(varchar(8),getdate(), 114)
    select datename(dw,'2004-10-15')
    select the week of the year=datename(week,'2004-10-15'), today is the day of the week=datename(weekday,'2004-10-15 ')
    SQL Server date function parameter/function
    GetDate() returns the current date and time of the system
    DateDiff (interval,date1,date2) returns the difference between date2 and date1 in the manner specified by interval date2-date1
    DateAdd ( interval,number,date) In the manner specified by interval, add the date after number
    DatePart (interval,date) returns the integer value corresponding to the specified part of the
    interval DateName (interval,date) returns the date in the date specified by interval Part of the corresponding string name

The setting values ​​of the parameter interval are as follows:
Value abbreviation (Sql Server) (Access and ASP) Description
Year Yy yyyy Year 1753 ~ 9999
Quarter Qq q Season 1 ~ 4
Month Mm m Month 1 ~ 12
Day of year Dy y Number of days in a year ,
Day of the year 1-366 Day Dd d day, 1-31
Weekday Dw w Day of the week, day of the week 1-7
Week Wk ww Week, week of the year 0 ~ 51
Hour Hh h 0 ~ 23
Minute Minute Minute 0 ~ 59
Second Ss s Second 0 ~ 59
Millisecond Ms-Millisecond 0 ~ 999 In
Access and Asp, use date() and now() to obtain the system date and time; among them, DateDiff, DateAdd, DatePart can also be used in Access and asp, and the usage of these functions is similar.

Examples of SQL Server date functions:
1. GetDate() is used in sql server: select GetDate()
2. DateDiff('s','2005-07-20','2005-7-25 22:56:32') returns The value is 514592 seconds
DateDiff('d','2005-07-20','2005-7-25 22:56:32') The return value is 5 days
3. DatePart('w','2005-7-25 22:56:32') The return value is 2 that is Monday (Sunday is 1, Saturday is 7)
DatePart('d','2005-7-25 22:56:32') The return value is 25, which is 25 No.
DatePart('y','2005-7-25 22:56:32') The return value is 206, which is the 206th day of the year
DatePart('yyyy','2005-7-25 22:56:32' ) The return value is 2005 or 2005

Guess you like

Origin blog.csdn.net/asdasd1fdsyrt/article/details/109352728