Example
Return the year part of a date:
SELECT YEAR('2017/08/25') AS Year;
Definition and Usage
The YEAR() function returns the year part for a specified date.
Syntax
YEAR(date)
Return the year part of a date:
SELECT YEAR('2017/08/25') AS Year;
The YEAR() function returns the year part for a specified date.
YEAR(date)
Return the date and time of the SQL Server:
SELECT SYSDATETIME() AS SysDateTime;
The SYSDATETIME() function returns the date and time of the computer where the SQL Server is running.
SYSDATETIME()
Return the month part of a date:
SELECT MONTH('2017/08/25') AS Month;
The MONTH() function returns the month part for a specified date (a number from 1 to 12).
MONTH(date)
Check if the expression is a valid date:
SELECT ISDATE('2017-08-25');
The ISDATE() function checks an expression and returns 1 if it is a valid date, otherwise 0.
ISDATE(expression)
Return the current UTC date and time:
SELECT GETUTCDATE();
The GETUTCDATE() function returns the current database system UTC date and time, in a ‘YYYY-MM-DD hh:mm:ss.mmm’ format.
GETUTCDATE()
Return the current database system date and time:
SELECT GETDATE();
The GETDATE() function returns the current database system date and time, in a ‘YYYY-MM-DD hh:mm:ss.mmm’ format.
Tip: Also look at the CURRENT_TIMESTAMP function.
GETDATE()
Return the day of the month for a date:
SELECT DAY('2017/08/25') AS DayOfMonth;
The DAY() function returns the day of the month (from 1 to 31) for a specified date.
DAY(date)
Return a specified part of a date:
SELECT DATEPART(year, '2017/08/25') AS DatePartInt;
The DATEPART() function returns a specified part of a date.
This function returns the result as an integer value.
DATEPART(interval, date)
Return a specified part of a date:
SELECT DATENAME(year, '2017/08/25') AS DatePartString;
The DATENAME() function returns a specified part of a date.
This function returns the result as a string value.
DATENAME(interval, date)
Return a date from its parts:
SELECT DATEFROMPARTS(2018, 10, 31) AS DateFromParts;
The DATEFROMPARTS() function returns a date from the specified parts (year, month, and day values).
DATEFROMPARTS(year, month, day)