I need help with extracting certain parts from text data stored in my SQL Server table. Each row contains different string patterns and I want to pull out only the portions that begin with ‘CS’.
Sample data in my column:
CS-NO2-T6082 BROWN,STORAGE
CS-NO2-T6082 BROWN
CS-CT2N64045,POW REC X 2,ROCKER
CONSOLE,CS-CT2N74045,POW REC X 2
CS
CS,MAN REC
CS-T6082,POW REC X 2
CS-CT12N1176
CS-NO2-T6082 BROWN,SQUARE
CS-CT12N1176
CS-2T1176 GREY
What I’m trying to achieve:
I want to extract only the segments that start with ‘CS’. The challenge is that ‘CS’ can appear alone or followed by numbers and letters separated by dashes, sometimes with colors after a space. These segments can be at the start, middle, or end of the string.
My approach is to first detect if there are multiple values by looking for commas. Single values with ‘CS’ are easy to handle, but multiple comma-separated values make it tricky.
Expected output:
CS-NO2-T6082 BROWN
CS-NO2-T6082 BROWN
CS-CT2N64045
CS-CT2N74045
CS
CS
CS-T6082
CS-CT12N1176
CS-NO2-T6082 BROWN
CS-CT12N1176
CS-2T1176 GREY
Any SQL Server solutions would be greatly appreciated.