I have a large number of rows in an SQL Server 2008 db
For each row I have 3 columns that I care about
A typical row looks like this:
AccountNumber | basecode | subcode
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9689787209368901 | AQTXG AQTXG AQTXG AQTXG ACC5Z | ZQ596 ZQ596 ZQ596 ZQ655 ZC655
I need an SQL query that turns it into the following:
AccountNumber | basecode| subcode
+++++++++++++++++++++++++++++++++++++++++++
9689787209368901 | AQTXG | ZQ596
9689787209368901 | AQTXG | ZQ596
9689787209368901 | AQTXG | ZQ596
9689787209368901 | AQTXG | ZQ655
9689787209368901 | ACC5Z | ZC655
Since you are using SQL Server, you can use a recursive query to split the data. IN this query, you can have an unknown number of items that are split with the space (
' '), if your delimiter changes then this is easy to alter. Your query would be similar to this:See SQL Fiddle with Demo
The result is: