I have a column containing dates in the format yyyy-mm-dd
. Now I'm running a SELECT
query that converts that date to dd/mm/yyyy
using TO_CHAR(dob :: DATE, 'dd/mm/yyyy') AS dob
which works just fine. Now the problem I've encountered is that there are some bad records in that column, below is a sample table with a good record and a bad record:
| id | dob |
|----|------------|
| 1 | 2019-12-31 | // this returns 31/12/2019
| 2 | 31-12-2019 | // BAD RECORD, this returns an error
|----|------------|
The error I get on id 2 is:
ERROR: date/time field value out of range: "31-12-2019"
HINT: Perhaps you need a different "datestyle" setting.
SQL state: 22008
What I'd like is to conditionally check if the TO_CHAR(dob :: DATE, 'dd/mm/yyyy')
is okay otherwise just use the dob without conversion. Any way to pull that off?
I'm using Postges 12