Question

I have column declared
time_stamp | timestamp| NO| PRI| CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP.

I have some old data which I would like to use for testing posrpoises. Since my applciation defaults to displaying today's data, I would like an SQL command which changes the yyyy/nn//dd part to today, while retaining the hh:mm:ss part.

As the column is of type timestamp, I suspect I want something like

UPDATE table SET time_stamp = time_stamp + (24*60*60* (now() - timestamp)) or similar, but I am unsure of the exact command.

I can haz halp?


 show create table positions;

| positions | CREATE TABLE `positions` (
  `vehicle_id` int(11) NOT NULL,

  `time_stamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

  `latitude` double NOT NULL,
  `longitude` double NOT NULL,
  `distance_travelled` double NOT NULL,
  `address` varchar(128) NOT NULL,
  UNIQUE KEY `vehicle_id` (`vehicle_id`,`time_stamp`),
  CONSTRAINT `positions_ibfk_1` FOREIGN KEY (`vehicle_id`) REFERENCES `vehicles` (`vehicle_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |


select * from positions;

|          6 | 2013-10-22 17:24:57 | 1.33793333333333 | 103.755233333333 |               8483 | Pan Island Expressway, Singapore
                      |
|          6 | 2013-10-22 17:29:42 | 1.35208333333333 | 103.716433333333 |               7023 | 551 Jurong West Street 42, Singapore 640551
                      |
|          6 | 2013-10-22 17:34:42 | 1.35188333333333 | 103.715416666667 |                149 | 542 Jurong West Avenue 1, Singapore 640542
Was it helpful?

Solution

Something like this may work:

UPDATE table SET time_stamp = UNIX_TIMESTAMP(CONCAT(CURDATE(), ' ', TIME(FROM_UNIXTIME(time_stamp))))

FROM_UNIXTIME() will convert the unix timestamp into a datetime stamp which you can then easily extract the time portion by using TIME(). Then you can concatenate the current date to it by using CONCAT() with CURDATE() which gets the current date and finally UNIX_TIMESTAMP() which turns it back into a timestamp.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top