Precision required

  • mtassin (3/14/2013)


    Toreador (3/14/2013)


    Most disappointing. I was sure when I got to the thread I'd see several pages of identical replies saying that it gives an error about assigning variables in the declare statement. What is the world coming to?!

    I think most of us use SQL 2008 or later versions and are used to it by now. 🙂

    That said, this reminds me why I don't use smalldatetime for anything.

    Date - yes (just having dates is awesome!)

    Time - yes (just having times is also awesome!)

    Datetime - yes by default from old stuff (not the coolest, but it generally works)

    DateTimeOffset - default for new date/time columns (we have enough localization problems, storing the offset in the datetime helps us avoid new ones).

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

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

  • 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 6 posts - 31 through 35 (of 35 total)

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