The TSQL DATEPART Function

Download PDF

You can use the TSQL DATEPART function to extract individual parts of the date from a date or time item.

For instance:

SELECT DATEPART(year, GETDATE());

Will return the following output:

DatePart1

 

The second parameter is the date/time element which can be any of these types: time, date, smalldatetime, datetime, datetime2, or datetimeoffset.

 

Lets take a look at the possible options for the first parameter.

 SELECT DATEPART(year, GETDATE()) AS [year],
		DATEPART(quarter, GETDATE()) AS [quarter],
		DATEPART(month, GETDATE()) AS [month],
		DATEPART(dayofyear, GETDATE()) AS [dayofyear],
		DATEPART(day, GETDATE()) AS [day],
		DATEPART(week, GETDATE()) AS [week],
		DATEPART(weekday, GETDATE()) AS [weekday],
		DATEPART(hour, GETDATE()) AS [hour],
		DATEPART(minute, GETDATE()) AS [minute],
		DATEPART(second, GETDATE()) AS [second],
		DATEPART(millisecond, GETDATE()) AS [millisecond],
		DATEPART(microsecond, GETDATE()) AS [microsecond],
		DATEPART(nanosecond, GETDATE()) AS [nanosecond],
		DATEPART(ISO_WEEK, GETDATE()) AS [ISO_WEEK];

DatePart2

See Also

Tagged with:

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Time limit is exhausted. Please reload CAPTCHA.