1

My data set is changing and now includes two additional words at the start of my customer name field, I need to clean this data up before moving it to my main customer table.

What I need to be able to do is keep only the words after the second space in a select statement.

Can anyone suggest a way to do this

i.e. "ZENDUSER ABCABC S ROCCO AL PORTO" needs to be returned as "S ROCCO AL PORTO"

1
  • Thanks to everyone who has responded to my plea for help Commented Mar 5, 2018 at 11:33

4 Answers 4

2

You can use CHARINDEX and SUBSTRING to do this:

declare @a varchar(200)

set @a = 'ZENDUSER ABCABC S ROCCO AL PORTO'

select  @a, substring(@a, charindex(' ', @a, charindex(' ', @a, 1) + 1) + 1, 200)
Sign up to request clarification or add additional context in comments.

Comments

0
DECLARE @cust NVARCHAR(MAX);
SET @cust = N'ZENDUSER ABCABC S ROCCO AL PORTO';

SELECT SUBSTRING(@cust, CHARINDEX(' ', @cust, CHARINDEX(' ', @cust, 0) + 1) + 1,
                  LEN(@cust) - CHARINDEX(' ', @cust, CHARINDEX(' ', @cust, 0) + 1));
 GO


| (No column name) |
| :--------------- |
| S ROCCO AL PORTO |

dbfiddle here

Comments

0

Try this:

select substring(MyColumn, CHARINDEX(MyColumn, ' ', CHARINDEX(MyColumn, ' ', 1) + 1) + 1, Len(MyColumn)) from MyTable

I know, that this is very similair to MJH answer, but additionally, I take Len(MyColumn) in substring method, so we are sure that we include all characters after second space. The other answer takes only 200 characters.

2 Comments

My answer uses 200 characters because the varchar is defined with a max length of 200 characters, you can use any value you want in there (as long as it is at least equal to the max length) and my solution will work.
That is why it's better to use length of a string there ;)
0

If you'd like your trimming to be more dynamic i.e starting from the 5th word etc, you can use the following code snippet. I would have encapsulate this in an inline function for additional capabilities

    DECLARE @Sentence NVARCHAR(200) = 'ZENDUSER ABCABC S ROCCO AL PORTO'
    DECLARE @Del NVARCHAR(2)= ' '
    DECLARE @WordStart INT = 5

    ;WITH Nums (n) as 
        (
        SELECT TOP (LEN(@Sentence)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
        FROM (VALUES (0),(0),(0),(0),(0),(0),(0),(0)) a(n)
        CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b(n)
        CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) c(n)
        CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) d(n)
        )
    , Main as 
    (
    SELECT N, x.val , Ind , CASE WHEN n = 1 THEN 1  ELSE SUM(Ind) OVER (ORDER BY n) + 1  END Pos
    FROM Nums
        CROSS APPLY 
            (
            VALUES (SUBSTRING(@Sentence,n,1),
            CASE WHEN SUBSTRING(@Sentence,n,1) = @Del THEN 1 ELSE 0 END) 

            ) x(val, Ind)
    )
    , Combine (StrOut) as 
    (
    SELECT LTRIM(RTRIM(STUFF(
            CAST((SELECT ''+ val
            FROM Main
            WHERE Pos >= @WordStart
            FOR XML PATH (''),TYPE) AS NVARCHAR(MAX)),1,0,'')
            )))
     SELECT StrOut 
     FROM Combine

UPDATE: creating a function

    CREATE FUNCTION dbo.SentenceSplitter 
    (
    @Sentence NVARCHAR(2000),
    @WordStart INT,
    @Del NVARCHAR(2) = ' ' 
    )
    RETURNS TABLE AS RETURN

        WITH Nums (n) as 
                (
                SELECT TOP (LEN(@Sentence)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
                FROM (VALUES (0),(0),(0),(0),(0),(0),(0),(0)) a(n)
                CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b(n)
                CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) c(n)
                CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) d(n)
                )
            , Main as 
            (
            SELECT N, x.val , Ind , CASE WHEN n = 1 THEN 1  ELSE SUM(Ind) OVER (ORDER BY n) + 1  END Pos
            FROM Nums
                CROSS APPLY 
                    (
                    VALUES (SUBSTRING(@Sentence,n,1),
                    CASE WHEN SUBSTRING(@Sentence,n,1) = @Del THEN 1 ELSE 0 END) 

                    ) x(val, Ind)
            )
            , Combine (StrOut) as 
            (
            SELECT LTRIM(RTRIM(STUFF(
                    CAST((SELECT ''+ val
                    FROM Main
                    WHERE Pos >= @WordStart
                    FOR XML PATH (''),TYPE) AS NVARCHAR(MAX)),1,0,'')
                    )))
             SELECT StrOut 
             FROM Combine

Use case:

SELECT StrOut
FROM dbo.SentenceSplitter ('ZENDUSER ABCABC S ROCCO AL PORTO', 5, ' ')

will result:

StrOut
AL PORTO

3 Comments

OK, that could be useful - I'll need to work with it to see what it is doing. I take it the function doesn't work as a cross apply by adding a variable to the function where you put in the word to start with ie StrOut(2) to return everything from the 2nd space or StrOut(3) to return everything from the 3rd space.
I'll add another section of how to encapsulate it as a function and how to use it
I've edit the script with additional creation of the function and how to use it' let me know if it helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.