When I try to insert into a temporary table a decimal, I get an arithmetic overflow error:
Arithmetic overflow error converting varchar to data type numeric.
This is the SQL:
CREATE TABLE #ObDiabetesEncounterIDs
(
PatientEncounterID int,
Value decimal(5,2)
)
INSERT INTO #ObDiabetesEncounterIDs(PatientEncounterID, Value)
SELECT PatientEncounterID, Value
FROM Observation
WHERE Term = 'HGBA1C'
AND ISNUMERIC(Value) = 1
AND Value IS NOT NULL
AND PatientEncounterID IS NOT NULL;
Most of the decimal values are between 5.1 to 15.1. There are a few values that are whole numbers like 15 and 10. There are 2 that are out of that range such as 1405 and 151. I thought I accommodated these values by using the data type decimal(5,2).
Where am I going wrong?
UPDATE Updating the precision of the decimal helped. This accounted for the 1 record that was 1403. But there is also 1 record that has a comma instead of a period. So, the value is 9,7. So, I believe I will need to check for ISNUMERIC then change any commas to periods, then cast as a decimal.
DECIMAL(5,2)has 5 digits overall, of which 2 are after the decimal point - so you can store values from 0.00 to 999.99 in that type - a value like1405CANNOT be stored in such a decimal column!