Tuesday, April 23, 2013

Extract Excel input string "+"

DECLARE @test varchar(100) = '+HARIA+123+MALE+STUDENT+HOUSEWIFE+jhgjh+54545+hgdfjhg+xxxxc'

SELECT TOP 8000
    Num
INTO
    #Number
FROM
    (
    SELECT
       ROW_NUMBER() OVER (ORDER BY c1.object_id) AS Num
    FROM
       sys.columns c1, sys.columns c2, sys.columns c3
    ) N

SELECT
    ROW_NUMBER() OVER (ORDER BY Num) AS Rank,
    LTRIM(RTRIM(SUBSTRING(@test,
                          Num,
                          CHARINDEX('+', @test + '+', Num) - Num
                ))) AS Value
FROM
    #Number
WHERE
    Num <= LEN (@test)
    AND
    SUBSTRING('+' + @test, Num, 1) = '+'

DROP TABLE #Number
Source :stackoverflow

No comments: