0

Hi I have a mysql table that looks like so:

date        time
2009-07-31  02:30
2009-07-31  02:45
2009-07-31  03:00

'date' column is a date field and 'time' column is a text field.

I would like to, using mysql only, merge these into a third column, 'datetime' which is a timestamp field.

So the end result would look like this:

date        time     datetime
2009-07-31  02:30    2009-07-31 02:30
2009-07-31  02:45    2009-07-31 02:45
2009-07-31  03:00    2009-07-31 03:00

Not sure how to go about this. Any suggestions?

2 Answers 2

2

Easy...

ALTER TABLE yourTable ADD COLUMN `datetime` timestamp;

UPDATE yourTable SET `datetime` = CONCAT(`date`, '  ', `time`);

Although I might add that reserved keywords like date, time and datetime are not good column names.

Sign up to request clarification or add additional context in comments.

Comments

1

MySQL does have some 'loose type' behavior, so you can merge them as strings, and set the resulting string to the date-time column.

update `my_table` set `datetime`=concat(`date`, ' ', `time`);

Not all of the back-ticks are needed, but used to clarify the query.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.