In my database table, I am storing current date and time using NOW()
function. The column type is DATETIME
and it is storing in the format of YYYY-MM-DD HH:MM:SS
.
I am trying to write a query that selects the date and time from my table column and adding HH:MM to retrieved date and time like
SELECT date FROM tablename by adding fetched date which is (YYYY-MM-DD HH:MM:SS) + HH:MM
The above query is just to make you know what I was trying.
Actually I don't know how to add date and time in mysql while retrieving the data.
How can we add YYYY-MM-DD HH:MM:SS
and HH:MM
while retrieving the data in mysql.
You can use ADDTIME
in MySQL:
SELECT ADDTIME('2016-07-15 18:00:00', '02:30');
or with DATEADD
SELECT DATE_ADD('2016-07-15 18:00:00', INTERVAL '02:30' HOUR_MINUTE);
Both Result as:
2016-07-15 20:30:00
you don't do anything special:
SELECT ... WHERE datetimefield = '2016-07-15 13:13:00'
mysql will parse the string to see if it looks like a valid date/datetime value and convert it for you automatically.
For date "math", you can do that directly as well:
mysql> select '2016-07-16 13:13:00' + INTERVAL 3 hour + INTERVAL 15 minute;
+--------------------------------------------------------------+
| '2016-07-16 13:13:00' + INTERVAL 3 hour + INTERVAL 15 minute |
+--------------------------------------------------------------+
| 2016-07-16 16:28:00 |
+--------------------------------------------------------------+
1 row in set (0.00 sec)