sql MS SQL日期函数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql MS SQL日期函数相关的知识,希望对你有一定的参考价值。


DECLARE @date DATETIME  = GETDATE();
SELECT SYSDATETIME(),          -- 2019-09-01 22:15:09.0207887
       SYSDATETIMEOFFSET(),    -- 2019-09-01 22:15:09.0207887 +05:45
       SYSUTCDATETIME(),       -- 2019-09-01 16:30:09.0207887
       CURRENT_TIMESTAMP,      -- 2019-09-01 22:15:09.020
       GETDATE(),              -- 2019-09-01 22:15:09.020
       GETUTCDATE(),           -- 2019-09-01 16:30:09.020
       CONVERT (date, SYSDATETIME()),       -- 2019-09-01
       CONVERT (date, SYSDATETIMEOFFSET()), -- 2019-09-01 
       CONVERT (date, SYSUTCDATETIME()),    -- 2019-09-01
       CONVERT (date, CURRENT_TIMESTAMP),   -- 2019-09-01  
       CONVERT (date, GETDATE()),           -- 2019-09-01
       CONVERT (date, GETUTCDATE()),        -- 2019-09-01
       CONVERT (time, SYSDATETIME()),       -- 22:17:12.6010535
       CONVERT (time, SYSDATETIMEOFFSET()), -- 22:17:12.6010535 
       CONVERT (time, SYSUTCDATETIME()),    -- 16:32:12.6010535
       CONVERT (time, CURRENT_TIMESTAMP),   -- 22:17:12.6000000
       CONVERT (time, GETDATE()),           -- 22:17:12.6000000
       CONVERT (time, GETUTCDATE()),        -- 16:32:12.6000000
       DATEDIFF(day, DATEADD(day, -1, @date), @date), -- 1
       DATENAME(year, @date),               -- 2019
       DATEPART(year, '12:10:30.123'),      -- 1900
       YEAR(@date),                         -- 2019
       MONTH(@date),                        -- 9
       DAY(@date),                          -- 1   
       EOMONTH (@date),                     -- 2019-09-30
       ISDATE('2009-05-12 10:19:41.177')    -- 1


DECLARE @datetime2 datetime2 = '2007-01-01 13:10:10.1111111';  
SELECT DATEADD(millisecond,1,@datetime2),  -- 2007-01-01 13:10:10.1121111
       DATEADD(mcs,1,@datetime2),          -- 2007-01-01 13:10:10.1111121
	   DATEADD(nanosecond,49,@datetime2),  -- 2007-01-01 13:10:10.1111111
	   DATEADD(year,1,@datetime2),         -- 2008-01-01 13:10:10.1111111
	   DATEADD(month, 1, '20060830')       -- 2006-09-30 00:00:00.000

-- [Syntax]
-- DATEADD (datepart , number , date )  
-- DATEDIFF ( datepart , startdate , enddate )
-- DATENAME ( datepart , date )  
-- DATEPART ( datepart , date )  
-- EOMONTH ( start_date [, month_to_add ] )  
-- ISDATE ( expression )  

-- [DatePart]
-- year | yy | yyyy
-- quarter | qq | q
-- month | mm | m
-- dayofyear | dy | y
-- day | dd | d
-- week | wk | ww
-- weekday | dw | w
-- hour | hh
-- minute | mi | n
-- second | ss | s
-- millisecond | ms
-- microsecond | mcs
-- nanosecond | ns



-- DATETIME2FROMPARTS ( year, month, day, hour, minute, 
--                      seconds, fractions, precision )
SELECT DATETIME2FROMPARTS ( 2010, 12, 31, 23, 59, 59, 0, 0 )
-- Output: 2010-12-31 23:59:59


-- DATETIMEFROMPARTS ( year, month, day, hour, minute,
--                   seconds, milliseconds )  
SELECT DATETIMEFROMPARTS ( 2010, 12, 31, 23, 59, 59, 0 )
-- Output: 2010-12-31 23:59:59.000


-- DATETIMEOFFSETFROMPARTS ( year, month, day, hour, minute,
--                           seconds, fractions, hour_offset, 
--                           minute_offset, precision )  
SELECT DATETIMEOFFSETFROMPARTS ( 2010, 12, 31, 14, 23, 23, 0, 12, 0, 7 )
-- Output: 2010-12-31 14:23:23.0000000 +12:00


-- SMALLDATETIMEFROMPARTS ( year, month, day, hour, minute )  
SELECT SMALLDATETIMEFROMPARTS ( 2010, 12, 31, 23, 59 )
-- Output: 2010-12-31 23:59:00

-- SWITCHOFFSET ( DATETIMEOFFSET, time_zone )
-- select a datetimeoffset value into a time zone offset 
-- that is different from the time zone offset that was originally stored. 
SELECT switchoffset (CONVERT(datetimeoffset, GETDATE()), '-04:00'); 
-- 2019-09-01 18:36:16.6030000 -04:00

以上是关于sql MS SQL日期函数的主要内容,如果未能解决你的问题,请参考以下文章

MS SQL获取最大值或最小值日期的函数

Oracle SQL 两个日期类型毫秒值求差,日期转毫秒,时间差(ms)自定义函数解决毫秒差

MS Access/SQL 子查询的语法,包括聚合函数

日期转换问题 MS 访问 SQL Server

MS SQL Server 时间函数

在 MS SQL Server 中将字符串转换为日期时间