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

 

More from Stedman Solutions:

SteveStedman5
Steve and the team at Stedman Solutions are here for all your SQL Server needs.
Contact us today for your free 30 minute consultation..
We are ready to help!

Leave a Reply

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

*