• Jeff Moden (3/27/2015)


    ScottPletcher (3/27/2015)


    Eirikur Eiriksson (3/27/2015)


    ScottPletcher (3/27/2015)


    Just add them and SQL will implicitly convert the varchar to a datetime anyway:

    SELECT datetime_column + time_varchar AS new_datetime, ...

    FROM ...

    Until the locale driven implicit conversion starts to produce 16:55 AM....;-)

    😎

    Hmm, how could that happen? SQL will first convert the varchar to a datetime, then add the two, right!?

    I agree with Scott. If the datatype is DATETIME, how will there be any "locale driven implicit conversion". Please post an example because, for now anyway, I have to say "Not possible" to run into such a problem.

    Ooops, that came out slightly differently than I meant, the point I wanted to make was simply the hidden risk when relying on implicit conversions.

    😎

    One of the things I enforce is to adhere to BOL guidelines on International Transact-SQL by always using CONVERT with an explicit style parameter when working with date and time, in fact do I reject code that doesn't. Far too often have I had to deal with errors caused by this, sometimes very hard to spot or reproduce.

    For fun, 8:04AM cuts of the seconds.

    SELECT CONVERT(VARCHAR(8),CONVERT(TIME(0),'08:04:42',0),127) UNION ALL

    SELECT CONVERT(VARCHAR(8),CONVERT(TIME(0),'08:04:42',0),0)

    08:04:42

    08:04:00