Skip to content
Advertisement

Query won’t convert a nvarchar(30) to a decimal

I have the following query, that when it is run gives the following error:

The query is as follows:

Here is a fiddle: DB Fiddle

The only difference between the fiddle (which works fine) and the query here is that I have to have the ChannelValue column NVARCHAR as some of the data will be in text. I have filtered out all of the text data in the query.

I have also tested it with the following query and the data doesn’t show up. If it wasn’t a number I would expect it to appear here.

The expected outcome is that the number ‘247.300’ is converted into a number and processed. Why won’t it convert?

Advertisement

Answer

If you read the error message it complains about int.

this is from the implicit cast here

Use an explicit cast

then values such as 247.300 will convert fine and be comparable to the integer 0.

Though you also say “I have filtered out all of the text data in the query.”. In that case you should always use TRY_CONVERT anyway in case operations get ordered such that the conversion happens before the filter.

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