Combine two integer values to produce a month and year of birth, separated with a '/'

72 views Asked by At

I am converting a SQL Server stored procedure to a Postgres function. Two of the columns in the SQL Server table are MonthOfBirth and LifeOfBirth. They are both integer values. In SQL Server it is easy to combine these two columns like this:

CAST(MonthOfBirth AS VARCHAR(2)) + '/' + CAST(YearOfBirth AS VARCHAR(4))

I thought that maybe tochar() would work in Postgres, but it doesn't.

Could someone point me in the right direction?

Thanks

1

There are 1 answers

1
AudioBubble On

The direct translation of the SQL Server code is to use the standard concatenation operator ||

CAST(monthofbirth AS VARCHAR(2)) || '/' || CAST(yearofbirth AS VARCHAR(4))

But this is a bit easier using concat():

concat(monthofbirth, '/', yearofbirth)

If you need a leading zero for the month, use to_char()

concat(to_char(monthofbirth, 'FM00'), '/', yearofbirth)