Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update only the hour from a DATETIME field in MySQL?

I want to update a DateTime column where it will change the hours only without modifying anything else. I can't use to add an interval as the values all are different. There is many different dates too. So need to change the hour of exact desired date as where condition.

Ex:

*************************************************
**        Before       *|*         After       **
************************|************************
** 2017-07-24 19:06:15 *|* 2017-07-24 15:06:15 **
** 2017-07-24 17:12:23 *|* 2017-07-24 15:12:23 **
** 2017-07-24 23:00:03 *|* 2017-07-24 15:00:03 **
** 2017-07-24 20:33:56 *|* 2017-07-24 15:33:56 **
** 2017-07-24 18:19:31 *|* 2017-07-24 15:19:31 **
** 2017-07-24 16:43:47 *|* 2017-07-24 15:43:47 **
*************************************************

Want to do it with MySQL query only without using any programming language.

like image 825
M.A.K. Ripon Avatar asked Jul 04 '17 06:07

M.A.K. Ripon


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 I subtract hours in MySQL?

MySQL SUBTIME() Function The SUBTIME() function subtracts time from a time/datetime expression and then returns the new time/datetime.

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.

What is interval in MySQL?

MySQL interval is an operator, which is based on the binary search algorithm to search the items and returns the value from 0 to N. It is mainly used to calculate the date and time values. We can use the following syntax to create an interval value: INTERVAL expr unit.


2 Answers

You can use the DATE_FORMAT() function and "hardcode" the hour:

UPDATE some_table SET dt = DATE_FORMAT(dt, '%Y-%m-%d 15:%i:%s');

Demo: http://rextester.com/RJESF70894

If you want to bind the hour as parameter in a prepared statement, you can combine it with REPLACE():

UPDATE some_table SET dt = DATE_FORMAT(dt, REPLACE('%Y-%m-%d %H:%i:%s', '%H', ?))

Demo: http://rextester.com/OHUKF73552

like image 53
Paul Spiegel Avatar answered Sep 22 '22 06:09

Paul Spiegel


SQL

UPDATE datetimes
SET datetime = DATE_ADD(datetime,
                        INTERVAL (15 - HOUR(datetime)) HOUR);

Demo

http://rextester.com/JOJWJ94999

Explanation

DATE_ADD(datetime, INTERVALintervalHOUR) adds or subtracts interval hours from datetime (depending on whether interval is positive or negative). The number of hours to add or subtract is calculated by subtracting the number of hours part of datetime (found from HOUR(datetime)) from 15. If the current time is 16:00 or after, this will be negative and if the current time is before 15:00, it will be a positive number. There is no WHERE clause so all rows in the table will be updated.

like image 39
Steve Chambers Avatar answered Sep 23 '22 06:09

Steve Chambers