SQL to convert UTC date to datetime

  • Can someone tell me how to convert a utc time into a datetime for display purposes via SQL.

    Thanks

    Scott

     

     

  • UTC date times are usually just date times based on UTC location. If it is something else then what are you seeing?

  • The UTC date is held as decimal(20,0) ( ?? number of seconds since 19xx I think ) - I need to convert this ( via SQL ) to a display date - e.g. dd/mm/yyyyy hh:mm:ss.

     

    Thanks

  • You should post some input data along with what it should represent. That might be more descriptive.

     

     

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Sounds like you have an application running over these tables and you are reporting. Usually the date is the number of seconds since Jan 1 1970. All you need to do is the do a dateadd to get the right date.

    SELECT DATEADD(s,'19700101',colVal) FROM ...

    I also posted code for a procedure to Convrt GMT to local if you need. It can easily be converted to a user defined function.

  • IF you are using standard field storage for UTC then the below should work.  Please note that this code will return Greenwich Mean time (Zulu time for you pilots...)

     

    -- 1994-11-05T08:15:30-05:00

    -- 1994-11-05T13:15:30Z

    DECLARE @UTC  VARCHAR(25)

    DECLARE @Date DATETIME

    DECLARE @HH   SMALLINT

    DECLARE @mm   SMALLINT

    SET     @UTC = '1994-11-05T08:15:30-05:00'

    -- SET     @UTC = '1994-11-05T13:15:30Z'

    SET @Date = LEFT(@UTC, 10) + SPACE(1) + SUBSTRING(@UTC, 12, 8)

    IF RIGHT(RTRIM(LTRIM(@UTC)), 1) = 'Z'

      BEGIN

        SELECT @Date

      END

    ELSE

      BEGIN

        SET @HH   = -1 * SUBSTRING(@UTC, 20, 3)

        SET @mm   = -1 * SUBSTRING(@UTC, 20, 1) + SUBSTRING(@UTC, 24, 2)

        SET @Date = DATEADD(HOUR, @HH, @Date)

        SET @Date = DATEADD(MINUTE, @mm, @Date)

        SELECT @Date

      END



    Good Hunting!

    AJ Ahrens


    webmaster@kritter.net

  • Many thanks - it sounds like that will add 19,700,101 seconds to the date but i will give it a try.

  • Many thanks , will give it a try.

Viewing 8 posts - 1 through 8 (of 8 total)

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