Update only time from my Datetime field in sql

前端 未结 8 1660
旧时难觅i
旧时难觅i 2020-12-08 14:41

I have a date 2013-12-14 05:00:00.000 in my table.

Now i want to update only time of that date. E.G to 2013-12-14 04:00:00.000

Is there any query to update o

8条回答
  •  粉色の甜心
    2020-12-08 15:03

    In case of just updating a particular part of the datetime you can use SMALLDATETIMEFROMPARTS like:

    UPDATE MyTable 
    SET MyDate = SMALLDATETIMEFROMPARTS(YEAR(MyDate), MONTH(MyDate), DAY(MyDate), , ) 
    

    In other cases it may be required to copy parts of datetime to other or update only certain parts of the datetime:

    UPDATE MyTable 
    SET MyDate = SMALLDATETIMEFROMPARTS(YEAR(MyDate), MONTH(MyDate), DAY(MyDate), DATEPART(hour, MyDate), DATEPART(minute, MyDate)) 
    

    Refer SQL Server Date/Time related API references for more such functions

提交回复
热议问题