Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update only Time in a mysql DateTime field

Tags:

sql

mysql

How can I update only the time in an already existing DateTime field in MySQL? I want the date to stay the same.

like image 457
Martin Avatar asked Aug 25 '09 07:08

Martin


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 can update only date in datetime column of table in SQL Server?

Just use varchar and modify what you want in it without touch the time. In this example I use CONVERT(varchar(12), columnDatetime) to get a string with length of 12 characteres assuming a case of time with a format for example like "20:10:15.250".

What is difference between timestamp and datetime in MySQL?

The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts.

How do you change the timestamp?

Syntax – Update value to Current TimestampALTER TABLE table_name updates table schema. CHANGE column_name updates the column to. column_name TIMESTAMP NOT NULL defines the column as of datatype TIMESTAMP. DEFAULT CURRENT_TIMESTAMP sets the default value of the column to CURRENT_TIMESTAMP.


2 Answers

Try this:

UPDATE yourtable  SET yourcolumn = concat(date(yourcolumn), ' 21:00:00') WHERE Id = yourid; 
like image 123
Dani Avatar answered Oct 09 '22 10:10

Dani


Try this:

UPDATE t1 SET DateTimeField = CONCAT(DATE(DateTimeField),' 12:34:56'); 
like image 20
jp. Avatar answered Oct 09 '22 11:10

jp.