Stored procedure to parse a string

前端 未结 4 1203
小蘑菇
小蘑菇 2021-01-25 13:41

I need to write a stored procedure for which the input is a string.

The input string contains variable names and their values separated by pipeline delimiter like this:<

4条回答
  •  忘掉有多难
    2021-01-25 14:05

    Let's assume your input param is called @Text.

    DECLARE @Text varchar(255),
        @x varchar(255)
    
    SET @Text = 'Name=Praveen | City=Hyderabad | Mobile=48629387429| Role=User'
    
    -- Added to show how to account for non-trailing |
    SET @Text = @Text + ' | ';
    
    SET @x = LTRIM(RTRIM(substring(
             @Text,
             charindex('Name=', @Text) + LEN('Name='),
             charindex(' | ', @Text, charindex('Name=', @Text)) - LEN('Name=')
             )))
    
    SELECT @x
    

    Then just repeat this for @y, @z, @t change Name= to whatever your break is.

提交回复
热议问题