Search code examples
sqlsql-servert-sqldatehour

Sql by hour starting at noon


I'm currently using a select statement with one column as DATEPART(hh, CallTime) AS Hour and then doing:

GROUP BY DATEPART(hh, CallTime) 
ORDER BY Hour

This displays the hours starting at midnight and going through midnight - how would I go about having this go from noon to noon? Thanks!


Solution

  • CASE WHEN (DATEPART(hh, CallTime) >=12) THEN DATEPART(hh, CallTime) - 12 ELSE DATEPART(hh, CallTime)+12 END AS hour_since_noon should do it if I understood your question correctly.

    You may want to have 2 separate fields, your original one to actually display and this one to order