简体   繁体   中英

MySQL - Combining three columns into one with specific formatting

In my MySQL DB table I have a column called StartDate (of varchar type) plus three separate integer columns; Day, Month, Year.

What I want to do is combine the Day, Month and Year values and insert the resulting value into the StartDate field in the format dd/mm/yy (eg 03/07/84 ).

This wouldn't be too difficult but a lot of the Day and Month values are only one digit long, so those need to be prefixed with a 0 (eg 1 becomes 01, 9 becomes 09, 11 remains unaltered) in order to meet the required StartDate formatting.

The other problem is the Year field. This currently holds a 4 digit year, so the first 2 digits need to be deleted to meet the StartDate formatting (eg 1984 becomes 84, 2001 becomes 01).

Can anyone help work out the procedure I need to go through to achieve this. I'm guessing I need to work on the Day, Month and Year columns first to get them in the right formatting before then copying them and adding the '/' characters into the StartDate column.

Thanks a lot in advance for all help and assistance.

Try this query:

UPDATE `my_super_datestable` SET startdate = 
    date_format(str_to_date(concat_ws(
                          '/', `day`, `month`, `year`), '%e/%c/%Y'), '%d/%m/%y');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM