Skip to content
Advertisement

sql extract multiple words from one cell

I have a column that has some text written in it, including serial numbers. What I want to do is to extract the serial numbers , when they exists. I managed to extract only the first one. This works great if there is only one SN in it, otherwise the rest are not shown.

For example I have this:

I need to split the each SN into new row, like this:

This is what I did so far:

As it can be seen , the serial nums are in between tab and par.

I used par SN:tab – because I was trying to get any result

I am still a newbie when it comes to SQL, so any help would be great.

EDIT

using the answer from @Tyron78 I tried to get my result, but it still gives me errors. I do have to point out that I did change the code a little bit because I use MS SQL – instead of TRIM func I used RTRIM(LTRIM()) – which should work – but I still get the first part of the text in the result.

When the query is implemented on the first line I get the following result:

AND this is the msg I get : Message 9421, level 16, status 1, line 1 XML analysis: line 2, character 48, invalid name character.

Advertisement

Answer

You can try and use a mixture of XML and CrossApply, such as follows:

However, this will only work if SN is the last part of your string. If anything else follows in the string, you will have to consider this in your substring in cte1.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement