Skip to content
Advertisement

Conversion failed when converting the nvarchar value ‘$28,926.25’ to data type int

Full error message

Msg 245, Level 16, State 1, Line 2
Conversion failed when converting the nvarchar value ‘$28,926.25’ to data type int.

Advertisement

Answer

Something to understand when programming on almost ANY platform is converting between number values like 123.45 and string or text values like "123.45" is a surprisingly slow and expensive operation. They may look the same, but those are different values, and largely thanks to internationalization/cultural concerns the conversions between them are not trivial. Always seek to minimize these conversions, and when you do need them leave it until the last possible moment.

SQL Server is no exception here. It can be very picky about mixing between numbers and text.

For this question, the nested inner query takes the sum(soh.SubTotal) expression, which is a number, and wraps it in a FORMAT() function call, which creates a text field. We later try to use the result in the outer query as if it were a number again.

Don’t do that!

Wait to format the result until the SELECT clause of the outer statement, at the last possible moment. This allows us to remove the CAST()s from inside the CASE expression completely.

Also, NEVER use the old A,B WHERE join syntax. It’s been out of date for more than 25 years now.

Even better, don’t even convert to a string/text in the SELECT clause! Let the client program or reporting tool handle this part. This is another way we can follow the “leave the conversion to the last possible moment” guidance in the first paragraph.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement