/* Environment: win7 + SQL server 2008 r2 Author: CC Desctiption: Sort common date functions: DAY, MONTH, YEAR, DATEPART DATEADD, DATENAME, DATEDIFF, isdate usage */ -- Intercept a period of years, months, and days. Select DAY (GETDATE ()) , MONTH (GETDATE () -- when the parameter of the MONTH () function is an integer, all return an integer of 1, that is, SQL Server considers it as January 1, January 1900. , YEAR (GETDATE () -- The date value should be between January 1, 1753 and January 1, 9999. This is the date range recognized by the SQL Server System. Otherwise, an error occurs. -- Use the DATEPART function UNION ALL Select datepart (DAY, GETDATE ()) , DATEPART (MONTH, GETDATE ()) , DATEPART (YEAR, GETDATE ()) /* (No column name) 7 2012 7 2012 */ Select datename (DD, GETDATE () -- Return type: bytes /* (No column name) */ -- Use dateadd to obtain the next time or previous time and date Select DATEADD (DD, 1, getdate () -- next day of the current time , DATEADD (MM, 1, getdate () -- Next month of the current time , DATEADD (YY, 1, getdate () -- next year of the current time , DATEADD (DD,-1, getdate () -- The day before the current time , DATEADD (MM,-1, getdate () -- Previous month of the current time , DATEADD (YY,-1, getdate () -- The year before the current time -- 15:47:05. 663 15:47:05. 663 15:47:05. 663 15:47:05. 663 15:47:05. 663 15:47:05. 663 Select datediff (DD, '2017-07-11 15:03:11. 100', GETDATE () -- time difference -- 6 Select datename (dayofyear, GETDATE (), DATEDIFF (dd, '2017-01-01 ', getdate ()) -- 199 198 Select datename (year, GETDATE ()) , DATENAME (month, GETDATE ()) , DATENAME (day, GETDATE ()) , DATENAME (week, GETDATE ()) , DATENAME (dayofyear, GETDATE ()) , DATENAME (weekday, GETDATE ()); -- 2012 July 17 29 199 Tuesday Select datename (hour, GETDATE () -- The hour of the current day , DATENAME (minute, GETDATE () -- The fraction of the hour of the day , DATENAME (second, GETDATE (); -- the second of the hour minute of the day --- 15 48 9 Select isdate ('2014-07-17 ') UNION ALL Select isdate ('2014-07-17 15:12:00 ') UNION ALL Select isdate (111) /* */ /* Function syntax return value returned data type DAY (date) returns an integer that represents the "DAY" part of the specified date. Int is deterministic. MONTH (date) returns an integer that represents the "MONTH" part of the specified date. Int is deterministic. YEAR (date) returns an integer that represents the "YEAR" part of the specified date. Int is deterministic. DATENAME (datepart, date) returns the string representing the specified datepart of the specified date. Nvarchar DATEPART (datepart, date) returns an integer that represents the specified datepart of a specified date. Int DATEDIFF (datepart, startdate, enddate) returns the number of datepart boundaries between two specified dates. Int is deterministic. DATEADD (datepart, number, date) returns a new datetime value by adding a time interval to the specified datepart of the specified date. ISDATE (expression) determines whether the datetime or smalldatetime input expression is a valid date or time value. Int is only used with the CONVERT function. If the CONVERT style parameter is specified and the style is not equal to 0, 100, 9, or 109, ISDATE is determined. */ |