Precision required

  • David Burrows (3/19/2013)


    Shame they did not extend DATEADD to include a time option 🙁

    They didn't need to. DATEADD has always supported time options.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Hugo Kornelis (3/19/2013)


    David Burrows (3/19/2013)


    Shame they did not extend DATEADD to include a time option 🙁

    They didn't need to. DATEADD has always supported time options.

    Sorry I meant an option to add TIME data type instead of having to convert TIME to seconds and then adding to DATETIME data type or converted DATE data type.

    Far away is close at hand in the images of elsewhere.
    Anon.

  • David Burrows (3/19/2013)


    Sorry I meant an option to add TIME data type instead of having to convert TIME to seconds and then adding to DATETIME data type or converted DATE data type.

    For a minute I misunderstood you again, thinking that you wanted to be able to use DATEADD to add, for instance, two hours to a value of datatype TIME. Which, again, is possible.

    But then it dawned me that you want to be able to create a datetime value out of a data and a time. And you are right - there should be an easy way to do this, yet there isn't. I think this is the shortest you can do:

    DECLARE @d date = '20130320';

    DECLARE @t time = '13:05';

    SELECT DATEADD(second, DATEDIFF(second, '0:00', @t), CAST(@d AS datetime));


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • It was real good question. 🙂

  • Just on the edge

Viewing 5 posts - 31 through 35 (of 35 total)

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