SQL Server > Built in functions

DatePart function in SQL Server

How to get the integer part of a particular date in Sql Server?


To get integer part of a particular date, we can use DATEPART function in Sql Server.

select DATEPART(dw, '2015-05-19 12:59:30')

Output

3

This function can also be used to get Month number, year, day of the month etc. as shown below. Pass desired parameter in place of "dw" to get required details.

Parameter ValueDescription
yy 2015 Year
qq 2 Quarter
mm 5 Month number
dy 139 Day of the year
dd 19 Day of the month
wk 21 Week of the year
dw 3 Day number of the week
hh 12 Hour of the date time passed
n 59 Minute of the date time passed
ss 30 Second of the date time passed

Similarly, we can also pass ms for milli seconds, mcs for micro seconds, ns for nano seconds.

To get the name part from the date, we can use DATENAME function in SQL Server.

 Views: 6314 | Post Order: 68



Write for us






Hosting Recommendations