Sum aggregates rows, so first youll need to take your string and parse it into a table. Then you can use sum.Heres another way, you can put this into a function and call the same way youre currently calling the sum:declare @num varchar(10)set @num = '9999'select sum(len(@num) - len(REPLACE(@num, '1', ''))) * 1 + sum(len(@num) - len(REPLACE(@num, '2', ''))) * 2 + sum(len(@num) - len(REPLACE(@num, '3', ''))) * 3 + sum(len(@num) - len(REPLACE(@num, '4', ''))) * 4 + sum(len(@num) - len(REPLACE(@num, '5', ''))) * 5 + sum(len(@num) - len(REPLACE(@num, '6', ''))) * 6 + sum(len(@num) - len(REPLACE(@num, '7', ''))) * 7 + sum(len(@num) - len(REPLACE(@num, '8', ''))) * 8 + sum(len(@num) - len(REPLACE(@num, '9', ''))) * 9
Please post back and let us know how it worked!Nathan Skerl