Sql Server Series: date and time functions
1. Get the current system date function GETDATE ()
GETDATE () function returns the current date and time database systems, the return type is datetime.
SELECT GETDATE()
2. Return function GETUTCDATE UTC date ()
UTCDATE () function returns the current (UTC) date value UTC.
SELECT GETUTCDATE()
3. Obtain a function of the number of days DAY
DAY () function returns the specified date is the first day of the month, a range from 1 to 31. DAY with DATEPART (day, date) returns the same value.
grammar:
DAY ( date )
Example:
SELECT DAY(GETDATE())
SELECT DAY('2014-11-30')
4. Get function MONTH month
MONTH () function returns the integer value specified date in the month. MONTH Returns the value DATEPART (month, date) returned the same value.
grammar:
MONTH ( date )
Example:
SELECT MONTH(GETDATE())
SELECT MONTH('2014-11-30')
The function of acquiring the year YEAR
YEAR () returns an integer that represents the specified date of the year. YEAR and DATEPART (year, date) returns the same value.
grammar:
YEAR ( date )
Example:
SELECT YEAR(GETDATE())
6. The acquisition date specified string value function portion DATENAME
Returns the specified designated date of the string of datepart
grammar:
DATENAME ( datepart , date )
datepart possible values: year, quarter, month, day, dayofyear, week, weekday and so on.
Example:
SELECT DATENAME(YEAR, GETDATE()) SELECT DATENAME(MONTH, GETDATE()) SELECT DATENAME(WEEK, GETDATE()) SELECT DATENAME(WEEKDAY, GETDATE())
7. Get function DATEPART date specified integer value portion
Returns a specified specified date of integer datepart of.
grammar:
DATEPART ( datepart , date )
datepart possible values: year, quarter, month, day, dayofyear, week, weekday and so on.
Example:
SELECT DATEPART(YEAR, GETDATE()) SELECT DATEPART(MONTH, GETDATE()) SELECT DATEPART(WEEK, GETDATE()) SELECT DATEPART(WEEKDAY, GETDATE())
8. Calculate the date and time function DATEADD
After the specified time interval number (signed integer) of the specified date specified datepart adding the return date. The number can be negative.
grammar:
DATEADD (datepart , number , date )
datepart possible values: year, quarter, month, day, dayofyear, week, weekday, hour, minute, second and so on.
Example:
SELECT DATEADD(YEAR, 1, GETDATE())
1. Get the current system date function GETDATE ()
GETDATE () function returns the current date and time database systems, the return type is datetime.
SELECT GETDATE()
2. Return function GETUTCDATE UTC date ()
UTCDATE () function returns the current (UTC) date value UTC.
SELECT GETUTCDATE()
3. Obtain a function of the number of days DAY
DAY () function returns the specified date is the first day of the month, a range from 1 to 31. DAY with DATEPART (day, date) returns the same value.
grammar:
DAY ( date )
Example:
SELECT DAY(GETDATE())
SELECT DAY('2014-11-30')
4. Get function MONTH month
MONTH () function returns the integer value specified date in the month. MONTH Returns the value DATEPART (month, date) returned the same value.
grammar:
MONTH ( date )
Example:
SELECT MONTH(GETDATE())
SELECT MONTH('2014-11-30')
The function of acquiring the year YEAR
YEAR () returns an integer that represents the specified date of the year. YEAR and DATEPART (year, date) returns the same value.
grammar:
YEAR ( date )
Example:
SELECT YEAR(GETDATE())
6. The acquisition date specified string value function portion DATENAME
Returns the specified designated date of the string of datepart
grammar:
DATENAME ( datepart , date )
datepart possible values: year, quarter, month, day, dayofyear, week, weekday and so on.
Example:
SELECT DATENAME(YEAR, GETDATE()) SELECT DATENAME(MONTH, GETDATE()) SELECT DATENAME(WEEK, GETDATE()) SELECT DATENAME(WEEKDAY, GETDATE())
7. Get function DATEPART date specified integer value portion
Returns a specified specified date of integer datepart of.
grammar:
DATEPART ( datepart , date )
datepart possible values: year, quarter, month, day, dayofyear, week, weekday and so on.
Example:
SELECT DATEPART(YEAR, GETDATE()) SELECT DATEPART(MONTH, GETDATE()) SELECT DATEPART(WEEK, GETDATE()) SELECT DATEPART(WEEKDAY, GETDATE())
8. Calculate the date and time function DATEADD
After the specified time interval number (signed integer) of the specified date specified datepart adding the return date. The number can be negative.
grammar:
DATEADD (datepart , number , date )
datepart possible values: year, quarter, month, day, dayofyear, week, weekday, hour, minute, second and so on.
Example:
SELECT DATEADD(YEAR, 1, GETDATE())