Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Concatenate Year and Month

I have 2 date fields, surv_year (int) and surv_month (text).

Example outputs:

surv_year surv_month
2022      January
2022      August

Is there anyway to concatenate the 2 and change the month from a text to a number and have my final output as YYYYMM with my example above showing 202201 and 202208?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can convert the two values to a proper date using the to_date() function:

select to_date(concat(surv_month, ' ', surv_year), 'Month yyyy')
from the_table

will return a date on the first day of that month.

This date can be formatted using the to_char() function to the format that you want:

select to_char(to_date(concat(surv_month, ' ', surv_year), 'Month yyyy'), 'yyyymm')
from the_table
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading