Is it possible to use SUM function in a SQL query on a field that has been set as nvarchar but only contains numbers?
This is possible by casting:
SELECT SUM(CAST(NVarcharCol as int))
However, this will throw an exception is 1 or more records cannot be CAST
to an int
.
If your column is only ever going to have numerical values, I would recommend changing the data type to int to save future problems.
If you are not able to change the data type, then the following WHERE
clause would prevent exceptions from being thrown:
WHERE ISNUMERIC([NVarcharCol])=1
select sum (cast (myFiled as int)) from myTable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With