-5 Hours from HH:MM:SS

  • Hi,

    I need to get -5 hours from my time .

    Example 12:00:01 should be 07:00:01

    Thanks,

    PSB

  • Use DATEADD()

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • I am using DATEADD(HOUR,-5,Runtime) and am getting 1900-01-01 07:00:01.000

  • CAST the output as TIME

    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
  • Or the input.

    Or cast the output as string, but don't do this, unless you don't plan on doing anything else with this data.

    SELECT DATEADD(HOUR,-5, CAST('12:00:01' AS time)) AS cast_input,

    CAST(DATEADD(HOUR,-5, '12:00:01') AS time) AS cast_output,

    CONVERT(char(8), DATEADD(HOUR,-5, '12:00:01'), 108) AS cast_as_char;

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Thank You.

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

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