I have the following columns in my table:
launch_month Years
Jun 2018
July 2018
Aug 2018
I want to create a new column called 'launch_id' and combine the other 2 columns into a string of '201806', so the above would look like:
launch_month Years launch_id
Jun 2018 201806
July 2018 201807
Aug 2018 201808
Can anyone help?
One option uses a case
expression:
select
launch_month,
years,
years * 100 + case launch_month
when 'Jan' then 1
when 'Feb' then 2
when 'Mar' then 3
...
when 'Dec' then 12
end launch_id
from mytable
This generates a new numeric column called launch_id
, that represents the date in format YYYYMM
.
Alternatively, you can use date functions (but this might be less efficient):
select
launch_month,
years,
date_format(
str_to_date(concat('01', launch_month, years), '%d%b%Y'),
'%Y%m'
) launch_id
from mytable
This generates a string result. If you want a number, you can just convert it, either explicitly ( cast()
) or implicitly ( + 0
).
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.