Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading