This article describes some common time and date processing functions in SQL, including the-DATEPARTDATEDIFFCONVERTgetdate () DATEADD functions.
This article describes some commonly used time and date processing functions in SQL, including the-DATEPART DATEDIFF CONVERT getdate () DATEADD functions.
-- Get the current time
Select getdate ()
-- Obtain the current year, month, and day. YY indicates the year, MM indicates the month, DD indicates the day, hh indicates the day, and ss indicates the second.
/*
The Code is as follows: |
|
Year yy 1753--9999 quarter qq 1--4 month mm 1--12 day of year dy 1--366 Day dd 1--31 week wk 1--53 weekday dw 1--7 (Sunday -- Saturday) |
Hour hh 0--23 minute mi 0--59 second ss 0--59 milisecond MS 0--999 */
-- DATEPART achieves the same effect as DATENAME. DATEPART returns an integer and DATENAME returns a character.
The Code is as follows: |
|
Select DATEPART (qq, getdate ()) |
-- Get-d from the previous day; + d from the next day
The Code is as follows: |
|
Select getdate ()-1 |
-- Obtain the yy, mm, dd, and dd of the two time ranges.
The Code is as follows: |
|
Select DATEDIFF (dd, '2017-7-7 ', getdate ()) |
-- Convert a date of string type to a date type
The Code is as follows: |
|
Select CONVERT (DATETIME, '2017-8-6 ')) |
-- Add or subtract year, month, and day on the current time
The Code is as follows: |
|
Select DATEADD (mm,-2, getdate ()) |