Search code examples
sqlsql-servert-sqltextsum

SQL sum numbers in text


This is the code

select 'QWXWX/50,GNTGHNT/5,F/500'  

I want to see sum of the numbers in this texts. My expectation result is 555.


Solution

  • You shouldn't do this in T-SQL, but anyway you still could if you wanted to:

    with basedata (data) as 
    (
      select * from string_split('QWXWX/50,GNTGHNT/5,F/500',',')
    )
    select sum(cast(replace(data, rtrim(translate(data, '0123456789',space(10))), '') as int))  from basedata;
    

    DBFiddle demo