Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update only time from my Datetime field in sql

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 only time from datetime field?

like image 329
omkar patade Avatar asked Mar 29 '13 09:03

omkar patade


People also ask

How can change time in datetime field in SQL query?

To update with the current date and time: UPDATE table_name SET date_field = CURRENT_TIMESTAMP; To update with a specific date value: UPDATE table_name SET date_field = 'YYYY-MM-DD HH:MM:SS.

How do you update a specific record in SQL?

First, specify the table name that you want to change data in the UPDATE clause. Second, assign a new value for the column that you want to update. In case you want to update data in multiple columns, each column = value pair is separated by a comma (,). Third, specify which rows you want to update in the WHERE clause.

How do I keep just the year from a date in SQL?

If you need to store a year in the database, you would either want to use an Integer datatype (if you are dead set on only storing the year) or a DateTime datatype (which would involve storing a date that basically is 1/1/1990 00:00:00 in format). – KM. @KM.


1 Answers

UPDATE MyTable  SET MyDate = DATEADD(HOUR, 4, CAST(CAST(MyDate AS DATE) AS DATETIME))  

Or this

UPDATE MyTable  SET MyDate = DATEADD(HOUR, 4, CAST(FLOOR(CAST(MyDate AS FLOAT)) AS DATETIME)) 
like image 64
Yuriy Rozhovetskiy Avatar answered Sep 24 '22 11:09

Yuriy Rozhovetskiy