This nice function was taking multiple kinds of date inputs and normalizing them.
I assume it expected one of:
- blank which it let be null
- a date starting with 'MMM' in date format which would not pass '^\d.*' (i.e. something that doesn’t start with a number) which it would cast as a date
- a number
The reason that the date was being casted as an INT
is because after failing the first two tests the person writing this was expecting an INT
. They wanted to add the integer to the beginning of Time (i.e. 1900-01-01) like Excel does.
1999-09-07 16:30:00.000
fails the second test even though it could be cast as time.
This passes through to the else
, which fails to cast it as INT
, and throws the error.
In this case, you need to change your second test. Make it something that will allow a datetime that you have coming in, but that would reject a number that should be added to 1900-01-01
.
If you don’t think you will have numbers coming in that should be added to 1900-01-01
, then just get rid of the third test and use
select
case
when dbDate = '' then null
else dbDate::timestamp
end as dbDate