Functions That Return Date and Time Parts
The following functions return date parts:
DATENAME – returns a string corresponding to the datepart specified
DATEPART – returns an integer corresponding to the datepart specified
DAY – returns an integer corresponding to the day specified
MONTH– returns an integer corresponding to the month specified
YEAR– returns an integer corresponding to the year specified
DATENAME Function
-- date and time parts - returns nvarchar
SELECT DATENAME(YEAR, GETDATE()) AS 'Year';
SELECT DATENAME(QUARTER, GETDATE()) AS 'Quarter';
SELECT DATENAME(MONTH, GETDATE()) AS 'Month';
SELECT DATENAME(DAYOFYEAR, GETDATE()) AS 'DayOfYear';
SELECT DATENAME(DAY, GETDATE()) AS 'Day';
SELECT DATENAME(WEEK, GETDATE()) AS 'Week';
SELECT DATENAME(WEEKDAY, GETDATE()) AS 'WeekDay';
SELECT DATENAME(HOUR, GETDATE()) AS 'Hour';
SELECT DATENAME(MINUTE, GETDATE()) AS 'Minute';
SELECT DATENAME(SECOND, GETDATE()) AS 'Second';
SELECT DATENAME(MILLISECOND, GETDATE()) AS 'MilliSecond';
SELECT DATENAME(MICROSECOND, GETDATE()) AS 'MicroSecond';
SELECT DATENAME(NANOSECOND, GETDATE()) AS 'NanoSecond';
SELECT DATENAME(ISO_WEEK, GETDATE()) AS 'Week';
DATEPART Function
-- date and time parts - returns int
SELECT DATEPART(YEAR, GETDATE()) AS 'Year';
SELECT DATEPART(QUARTER, GETDATE()) AS 'Quarter';
SELECT DATEPART(MONTH, GETDATE()) AS 'Month';
SELECT DATEPART(DAYOFYEAR, GETDATE()) AS 'DayOfYear';
SELECT DATEPART(DAY, GETDATE()) AS 'Day';
SELECT DATEPART(WEEK, GETDATE()) AS 'Week';
SELECT DATEPART(WEEKDAY, GETDATE()) AS 'WeekDay';
SELECT DATEPART(HOUR, GETDATE()) AS 'Hour';
SELECT DATEPART(MINUTE, GETDATE()) AS 'Minute';
SELECT DATEPART(SECOND, GETDATE()) AS 'Second';
SELECT DATEPART(MILLISECOND, GETDATE()) AS 'MilliSecond';
SELECT DATEPART(MICROSECOND, GETDATE()) AS 'MicroSecond';
SELECT DATEPART(NANOSECOND, GETDATE()) AS 'NanoSecond';
SELECT DATEPART(ISO_WEEK, GETDATE()) AS 'Week';
DAY, MONTH and YEAR Functions
SELECT DAY(GETDATE()) AS 'Day';
SELECT MONTH(GETDATE()) AS 'Month';
SELECT YEAR(GETDATE()) AS 'Year';