Extracting hour and day from datetime

  • Is there a quick and easy way to extract just the hour and day from a column with the type of datetime?

    For example if the entry is...

    12/15/1997 11:59:00 PM

    ....I'd like to format it so that I get a 23 (military time) for the hour and Monday (or Mon) for the day result. I'd want the result in 2 separate columns.

    I figured out how to do this with Excel but I'd rather not mess with Excel on this project.

  • Try the DATEPART and DATENAME functions

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • I found this in BOL under DATEPART

    DECLARE @date DATETIME

    SET @date = '12/15/1997 11:59:00 PM'

    SELECT DATENAME(dw,@date),DATEPART(hh,@date)

    Greg
    _________________________________________________________________________________________________
    The glass is at one half capacity: nothing more, nothing less.

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

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