Amruta Raut Amruta Raut - 8 months ago 78
SQL Question

How to get duplicate substring and add remaining attach integer value of substring in DB2 sql

My scenario is that i have duplicate substring with string in column like


col1


|QWERT-1 |

|QWERT-3 |

|QWERT-12|

I want to get outout column as like

QWERT


| 16 |

i.e addition of 1+3+12 of duplicate substring(QWERT)and QWERT set as column name

How can I do it using DB2 SQL

Answer Source

Try this:

SELECT SUM(CAST(SUBSTR(t.YourColumn, LOCATE('-',t.YourColumn)+1) as INT)) as sum_total
FROM YourTable t
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download