So, I have a column of data, to use a previous example, body temperatures, that is stored as varchar so that no record gets rejected, however, it contains numeric data.
The people sending me the data are using a less than perfect system, so I have some incorrect data. What I need to do is write a SQL query to find valid values above or below a certain value.
For example, all temps over 104, which should indicate either extreme cases or errors.
I tried:
select count(1), result_num from VITALS where test_cd is 'TEMP' and cast(result_num as integer) > 104 group by result_num;
This returned an invalid number error, so I figured I had characters on some rows that couldn't convert to integers and I found to records with negative values ("-" before the number) and some that said "NULL", so I amended my query to read:
select count(1), result_num from VITALS where test_cd is 'TEMP' **and result_num not like '%-%' and result_num not like '%NULL%'** and cast(result_num as integer) > 104 group by result_num;
...and it still returned an invalid number error. I have triple checked the data in my RESULT_NUM field and those are the only character responses.
All other responses, whether legit temps or not, are numeric with no characters other than decimals.
Do I need to link the "not like" statements in parens or something?
This is probably a simple answer, but it is driving me nuts.