I have a varchar(100) column in a table that contains a mix of integers (as strings) and non-integer strings. E.g.
| dimension varchar(100) |
| '5' |
| '17' |
| '3' |
| 'Pyramids' |
| 'Western Bypass' |
| '15' |
How can I write an expression to, e.g. sum up all the values that are valid integers? If I were to try:
-- should return 5 + 17 + 3 + 15 = 40
SELECT
SUM( CONVERT( INT, dimension ) )
FROM
mytable
I would receive a Conversion failed when converting the varchar value 'Pyramids' to data type int.
error.
Is there a test I can use in my expression, much like the ISNULL()
function, that permits me to specify a default value if the field is not a number?