I am running an OpenQuery in SQL Server against a proprietary database which has unfortunately chosen to be overly modern and support 5 digit years and users being users do manage to record them. The code I have written is working, but gets unduly complicated as there are many dates in the vehicle table. My working code has the flavour of:
select
vehicle
, cast (recdate as varchar) as recdate
from OPENQUERY([TEST],
'SELECT
tbl.vehicle,
tbl.recdate
FROM vehicles tbl
WHERE (tbl.recdate <= {d ''9999-12-31''})
')
union
select
vehicle
, '5 digit date' as recdate
from OPENQUERY([TEST],
'SELECT
tbl.vehicle,
tbl.recdate
FROM Vehicles tbl
WHERE (tbl.recdate > {d ''9999-12-31''})
')
My attempts to test recdate outside of the openquery produce an "Error converting data type DBTYPE_DBDATE to date". I've tried both is_date() and try_cast(), but neither are designed to deal with a invalid date that is already in a date field.
Within the open query, I have tried using CONVERT, CAST and CASE statements, but these do not appear to be supported by the proprietary database.
Any suggestions?
Regards HamsterJam