TransWikia.com

Method to extract certain Text patterns from a column, between the space and comma

Stack Overflow Asked by user3537234 on February 19, 2021

I am trying to find the right way to extract certain patterns from a string and store them in a separate column.

Here is a example:

RAW DATA

Smith Pa, Coleman
John Pa-C, Fred
Justin DO, Jack
John OT, Press
Jack ARNP, Nate
Johm DPM, King

Desired Output

Pa
Pa-C
DO
OT
ARNP
DPM

There are titles for providers that i want to store in a seperate column.
Thank you

2 Answers

I've managed to safely extract the part of the string between the comma and the last space before it using a combination of stuff, charindex, reverse, len and cross apply.
I write "safely" because a stuff based solution will not raise errors if it tries to parse an ill-formatted string, unlike substring based solutions that will, as you've seen, throw an "Invalid length parameter passed to the LEFT or SUBSTRING function" error.

First, create and populate sample data (Please save us this step in your future questions):

DECLARE @T AS TABLE (
    Col varchar(50)
);

INSERT INTO @T (Col) VALUES
-- Valid examples
('Smith Pa, Coleman'),
('John Pa-C, Fred'),
('Justin DO, Jack'),
('John OT, Press'),
('Jack ARNP, Nate'),
('Johm DPM, King'),
-- Invalid examples
('NoSpaceTITLE, LastName'),
('No Comma TITLE LastName'),
-- Strange, but not really invalid example
('Van Damme ACTOR, Jean-Claude');

Note: I've added to the sample data some invalid examples and and also an example that isn't really invalid but is different than other examples.

The query:

SELECT    Col
        , NULLIF(STUFF(BeforeComma, 1, LEN(BeforeComma) - CHARINDEX(' ', REVERSE(BeforeComma)) + 1, ''), '') As SafeTitle
FROM @T
CROSS APPLY (SELECT STUFF(Col, CHARINDEX(',', Col), LEN(Col), '') As BeforeComma) As A

Note: using cross apply allowed me to calculate the string before the comma once, but use it three times in the query itself.

Results:

Col                             SafeTitle
Smith Pa, Coleman               Pa
John Pa-C, Fred                 Pa-C
Justin DO, Jack                 DO
John OT, Press                  OT
Jack ARNP, Nate                 ARNP
Johm DPM, King                  DPM
NoSpaceTITLE, LastName          NULL
No Comma TITLE LastName         NULL
Van Damme ACTOR, Jean-Claude    ACTOR

See a live demo on rextester

Correct answer by Zohar Peled on February 19, 2021

You can try the below if your actual data is exactly in the same format of your sample-

select substring(column1,charindex(' ',column1,1),
charindex(',',column1,1)-charindex(' ','column1,1)) as newcol from tablename

Answered by Fahmi on February 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP