Loading

Wednesday, November 18, 2009

How to get only hour from getdate() function in sql server

select datepart(hour,getdate()) as [hours]

DatePart Function will retrieve part of the date.

Syntax:
DATEPART(part, datetime)

Part
----
Ms for Milliseconds
Yy for Year
Qq for Quarter of the Year
Mm for Month
Dy for the Day of the Year
Dd for Day of the Month
Wk for Week
Dw for the Day of the Week
Hh for Hour
Mi for Minute
Ss for Second

You can replace part with the any of the part in the specified above items.

For example, for hours:

SELECT DATEPART(Hh,getdate())

No comments:

Post a Comment