Re: Vague dates and datetime types in sql server problems
Hi,
I'm trying to query our MSDE instance of Recorder 6. The VAGUE_DATE_START, VAGUE_DATE_END fields are stored as integers. I use
CONVERT( CHAR(10), CAST( VAGUE_DATE_START AS DateTime), 103)
to give me a DD/MM/YYYY string for display and reporting. However during a batch run, my client code reports arithmetic overflow errors on converting data type datetime. In our SURVEY_EVENT table (for example), I have the following stats for the VAGUE_DATE_START field;
VAGUE_DATE_START values > 0 = 51279
VAGUE_DATE_START values < 0 = 30
VAGUE_DATE_START values = 0 = 4
VAGUE_DATE_START values null = 21
--------
51334
The figure of 51334 is correct for the total number of rows in that table. This is where it gets odd. If I manually test the CAST function, say
SELECT CAST( -1 AS DateTime)
I get 31/12/1899. So the function works with negative numbers and with zero and null fields. But if I run this;
SELECT CAST( VAGUE_DATE_START AS DateTime)
FROM SURVEY_EVENT
WHERE VAGUE_DATE_START < 0
the query does not return a results set! However if I use NULL or > 0 or = 0, the query returns data, e.g.
SELECT CAST( VAGUE_DATE_START AS DateTime)
FROM SURVEY_EVENT
WHERE VAGUE_DATE_START > 0
The queries are being run using SQL Manager 2005 lite.
Not having a lot of experience with SQL server, am I doing the right thing here? Are there workarounds to this problem? Does anyone know how to format the VAGUE_DATE_* fields into something human readable?
Any help gratefully received. Thanks.
Update
After posting this, I checked out the code on another sql manager and it reported the overflow for number less than zero. The other SQL manager must be silent in its
errors :rolleyes:
I looked for the largest negative numbers in the table and found these;
-513528, -412160, -295282
These do not look right, and cause the CAST function to fail. This is either bad data or I am using the wrong approach to this problem.
Dave.
Biodiversity Technology Officer,
Biodiversity Information Service for Powys and Brecon Beacons National Park.