How can I select the hour from a datetime
?
I'm looking for the equivalent of Oracle's TO_CHAR( d, 'HH24' )
, which would return 17
for 2010-06-23 17:22:31
.
I tried to find out about the best time (best answer/questin-ration) to ask an SQL
-related question on StackOverflow, using Data Explorer. I got it to work, but I'm sure there is a more elegant version.
Select
Left( Convert(VARCHAR(10), creationDate, 8 ), 2 ) As hour,
Convert(Float, Count(*)) / Count(Distinct p.parentId) As ratio
From posts p
Join postTags pt On ( pt.postId = p.parentId )
Join tags t On ( t.id = pt.tagId )
Where p.postTypeId = 2 And t.tagName = '##TagName##'
Group By Left( Convert(VARCHAR(10), creationDate, 8 ), 2 )
Order BY hour
BTW, the best time seems to be between 15:00
and 16:00
- guess I'm asking too late :)