25

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

1
  • Will the year always remain the same?
    – rownage
    Jan 14, 2011 at 19:37

7 Answers 7

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').

1
  • 2
    Saved me some good stress. Works well with WHERE condition(s) too.
    – OmniPotens
    Jun 16, 2018 at 4:58
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)

1
  • 1
    For some reason, my answer wouldn't submit when i had the word "update" above spelled correctly. Weird.
    – rownage
    Jan 14, 2011 at 19:59
3

Probably, DATE_ADD is a good idea. link text

2

Check Query

 update yourtable set eventtime=replace(eventtime,substr(eventtime,1,10), '2013-07-17')  WHERE  `id`=4
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

1

Going to use something like:

CONCAT('2011-01-28 ',DATE_FORMAT(original_timestamp, '%H:%i:%s'))
0

Just Use:

UPDATE table_name set column_name= DATE_FORMAT(column_name,'%Y-%m-28 %H:%i-%s');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.