how to classify records based on time intervals

  • Hi, anybody knows if I have a table with a datetime column in SQL 2005, what would be the SQL statement(s) that would let me classify those records based on the hour

    For example if the hour of the datetime column is between

    6:30 and 9:00 print 'Morning'

    But if its between

    20:30 and 23:59 print 'Night'

    Thanks in advance

    Isaac

  • Use a case and datepart.

    select case when datepart(hh,getdate()) between 6 and 11 then 'morning'

    WHEN datepart(hh,getdate()) = 12 THEN 'noon'

    WHEN datepart(hh,getdate()) BETWEEN 13 and 17 THEN 'afternoon'

    end

    etc.

    Kenneth FisherI was once offered a wizards hat but it got in the way of my dunce cap.--------------------------------------------------------------------------------For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/[/url]Link to my Blog Post --> www.SQLStudies.com[/url]

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply