MySQL DATETIME - Change only the date
Asked Answered
D

7

26

Starting with : 2011-01-17 09:30:00

Let's say I want to edit just the date with 2011-01-28

What is the most efficient way to end up with: 2011-01-28 09:30:00

Thanks!

For everyone saying Date_Add... that would require me to subtract the dates, then add the days. Thats a possibility... but was looking to remove that first step, and just "replace" the date

Degroot answered 14/1, 2011 at 19:34 Comment(1)
Will the year always remain the same?Lingo
P
68

If you really don't want to use date_add function, you can consider using this construction:

UPDATE table_name SET field_name = concat('2011-01-12 ', time(field_name)) 

Make sure to add a space after the date ('2011-01-12').

Perfect answered 14/1, 2011 at 20:46 Comment(1)
Saved me some good stress. Works well with WHERE condition(s) too.Alberta
L
18

To change it 5 days ahead:

UPDATE yourTableName
SET myDate1 = myDate1 + INTERVAL 5 DAY
WHERE myDate1 = dateIWantToChange

(you can use MONTH, YEAR, etc too)

Lingo answered 14/1, 2011 at 19:57 Comment(1)
For some reason, my answer wouldn't submit when i had the word "update" above spelled correctly. Weird.Lingo
R
3

Probably, DATE_ADD is a good idea. link text

Rakia answered 14/1, 2011 at 19:39 Comment(0)
G
2

Check Query

 update yourtable set eventtime=replace(eventtime,substr(eventtime,1,10), '2013-07-17')  WHERE  `id`=4
Gavrila answered 17/7, 2013 at 10:29 Comment(0)
M
1

You can add various components of a date to modify it using the Date_Add function. Check this out:

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add

Melissamelisse answered 14/1, 2011 at 19:39 Comment(0)
D
1

Going to use something like:

CONCAT('2011-01-28 ',DATE_FORMAT(original_timestamp, '%H:%i:%s'))
Degroot answered 14/1, 2011 at 20:4 Comment(0)
C
0

Just Use:

UPDATE table_name set column_name= DATE_FORMAT(column_name,'%Y-%m-28 %H:%i-%s');
Coact answered 6/2, 2022 at 4:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.