locked
Add time to a date RRS feed

  • Question

  • I need to create a smalldatetime from a smalldatetime with no time and add the time from a GETDATE() value.  For example, if I have a smalldatetime value of 2018-12-05 00:00:00 and I want to add/replace the time of 00:00:00 with the hr:min:sec from the current value of GETDATE(), how do I do that?
    Monday, December 10, 2018 6:38 PM

Answers

  • You could simply try this:

    SELECT
    DATEADD(day,DATEDIFF(day,getdate(),smalldatetimecolumn)
        ,getdate())


    Russel Loski, MCSE Data Platform/Business Intelligence Twitter: @sqlmovers; blog: www.sqlmovers.com

    • Marked as answer by David Chase89 Monday, December 10, 2018 6:53 PM
    Monday, December 10, 2018 6:50 PM

All replies

  • You could simply try this:

    SELECT
    DATEADD(day,DATEDIFF(day,getdate(),smalldatetimecolumn)
        ,getdate())


    Russel Loski, MCSE Data Platform/Business Intelligence Twitter: @sqlmovers; blog: www.sqlmovers.com

    • Marked as answer by David Chase89 Monday, December 10, 2018 6:53 PM
    Monday, December 10, 2018 6:50 PM
  • declare @dt smalldatetime=' 2018-12-05 00:00:00'

    Select Cast(@dt as smalldatetime) + Cast(Cast(getdate() as time) as smalldatetime)
    Monday, December 10, 2018 6:56 PM