I have a column which is of String data type but holds a date value. I converted it to a DATE and then extracted the YEAR from it using the below format in POSTGRESQL. However, if I try to get the last 10 year data, I see that any date that is lesser than the last 50 years ( i.e less than 1970 , takes up the format of 2069, 2068 and so on). Lets say there is a date value in the format (10/1/58, it gets listed inspite of giving the filter for the last 10 years as 2058)
I used the below for converting to DATE and extracting the year
extract YEAR FROM TO_DATE(release_date,'MM/DD/YY')
Then used the below condition in my query to get the last 10 year data
where extract(year from current_timestamp)-EXTRACT(YEAR FROM TO_DATE(release_date,'MM/DD/YY')) <=10
How to solve this in POSTGRESQL
'MM/DD/YYYYinstead of'MM/DD/YY'. Per docs here Data formatting: 'In to_timestamp and to_date, if the year format specification is less than four digits, e.g., YYY, and the supplied year is less than four digits, the year will be adjusted to be nearest to the year 2020, e.g., 95 becomes 1995.'CASE WHENon the year and subtract 100 years when necessary. (And then you should change the column's data type toDATEin order to avoid such problems in the future.)DATE yyyy-mm-dd(ecample 1DATE '2072-01-01'`