Insert current time

  • Hi,

    I searched the forum for this topic but can't find a match.

    I have a column(varchar) that records the time of day a particular action occurs. The row is inserted with an sp that runs at a set time daily. The time is currently hardcoded in the sp. Grates my last nerve to do that.

    Besides, I want the flexibility to run this throughout the day.

    Here's my solution:

    case when datepart(hh, getdate()) < 10 then '0' + cast(datepart(hh, getdate()) as varchar)
    else cast(datepart(hh, getdate())as varchar) end +
    case when datepart(n, getdate()) < 10 then ':0' + cast(datepart(n, getdate()) as varchar)
    else ':' + cast(datepart(n, getdate())as varchar) end +
    case when datepart(ss, getdate()) < 10 then ':0' + cast(datepart(ss, getdate()) as varchar)
    else ':' + cast(datepart(ss, getdate())as varchar) end

    Not real pretty but it concatenates all the time segments nicely. Any other ideas on getting a time that doesn't drop the leading zero's in hours(0-24)/minutes/seconds? NOTE: I'm stuck with the varchar datatype for this table.

    Thanks,


    Greg H

  • select convert(varchar(12),getdate(),114)

     


    * Noel

  • Noel,

    I knew there had to be a quicker way. Just couldn't find it.

    Thanks,


    Greg H

  • Happy to Help


    * Noel

  • Surely holding the date/time in a varchar column should also grate ....

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

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