JGeorge
JGeorge

Reputation: 17

Cast giving an error

Why is this simple code giving me an error:

Conversion failed when converting the varchar value '3.0' to data type int.

select cast(value as INT)  
from CV3BasicObservation  (nolock)
where value >= 110

Upvotes: 0

Views: 338

Answers (1)

Brian Pressler
Brian Pressler

Reputation: 6713

SQL Server doesn't want to convert a number string that looks like a decimal to integer because you could lose precision. You could trick it with the round function:

select cast(round(value,0) as INT)  
from CV3BasicObservation  (nolock)
where cast(round(value,0) as INT) >= 110

NOTE: You have to do it to all instances of the field value where you are explicitly converting it to int or where it is implicitly converting it for comparison an int type value.

Upvotes: 5

Related Questions