I have a dataset of decimal values that are stored as varchar. I need to return all of the values that are less than 8.0. This is my query:
;WITH CTE
AS
(
SELECT PatientEncounterID, CAST(value as numeric(3,1)) As Value
from Observation
where Term = 'HGBA1C' AND isnumeric(value) = 1
)
SELECT *
FROM CTE
WHERE Value < 8.0
When I run this query I get the error:
converting data type varchar to numeric
When I run the query without the where clause the values range from 5.0 to 16.9.
The other column is just an identity field.
The Value field is converted to a number in the CTE query.
Why can the where clause error?
UPDATE The total rows is 228. There should be 144 returned but only 39 are returned.