Getting DATEPART of getdate()

  • Hi I am trying to extract just the hours and minutes form getdate. The best I have come up with is

    declare @date datetime

    set @date = getdate()

    select @date

    select DATEPART (HH,@date), DATEPART(MI,@date)

    however that outputs 2 separate columns. Any ideas how I can do this?

    Thanks

  • select CAST(DATEPART (HH,@date) AS VARCHAR(2))+':'+ CAST(DATEPART(MI,@date) AS VARCHAR(2))

  • and another way, since you posted in SQL2208, is to consdier the tIME datatype to get what yo are after as well:

    --returned 10:53:47.4970000 and 10:53 for me at that moment

    select

    CONVERT(time,getdate()),

    CONVERT(varchar(5),CONVERT(time,getdate()))

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Sorry, I missed a boundry value.

    IF LEN(CAST(DATEPART(MI,@date) AS VARCHAR(2))) < 2

    select CAST(DATEPART (HH,@date) AS VARCHAR(2))+':0'+ CAST(DATEPART(MI,@date) AS VARCHAR(2))

    ELSE

    select CAST(DATEPART (HH,@date) AS VARCHAR(2))+':'+ CAST(DATEPART(MI,@date) AS VARCHAR(2))

  • Thats great thanks guys

Viewing 5 posts - 1 through 4 (of 4 total)

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