Given day, month and year as integer columns in the table, calculate the date and weekending date from these values.
I tried the following but it gives me an incorrect result for the derived date as ‘2022-08-05 00:00:00’
select to_date(2020||03||20,'YYYYMMDD')
Even tried below but results in a string without leading zeros for month and day
select (cast (2020 as varchar)+cast (03 as varchar(2))+cast (02 as varchar(2)))
Result for above is : 202032
Advertisement
Answer
The problem is that you have numbers — so leading zeros are missing. You can try:
select to_date('2020'||'03'||'20', 'YYYYMMDD')
Or, if these have to be numbers:
select to_date( (2020 * 10000 + 03 * 100 + 20)::text, 'YYYYMMDD')