拆分字符串和存储

时间:2016-05-23 06:54:49

标签: sql

我有一个像这样的字符串

message_type=something, Hours=0, SQM=0, user_name=xxxxx, Boards=0, Charge=0, Proofs=0

我需要获得等于(=)(某事,xxxxx,0,0,0,0)之后的所有值,我们必须使用SQL将它们存储在变量中

3 个答案:

答案 0 :(得分:0)

如果我理解正确,你需要这样的东西吗?

SELECT
    RIGHT('message_type=something', (CHARINDEX('=', REVERSE('message_type=something'), 0)) - 1)

答案 1 :(得分:0)

请尝试以下代码。它与SQL Server 2012一起工作正常:

    BEGIN
          DECLARE @str VARCHAR(2000)
          DECLARE @input VARCHAR(4000)
          DECLARE @ind Int
          DECLARE @Count int 
          DECLARE @max int 
          SET @max =0

          DECLARE @Result TABLE (ID int identity(1,1),value varchar(1000))
          SET @input ='message_type=something, Hours=0, SQM=0, user_name=xxxxx, Boards=0, Charge=0, Proofs=0'
          IF(@input is not null)
              BEGIN
                    SET @ind = CharIndex(',',@input)
                    WHILE @ind > 0
                        BEGIN
                              SET @str = SUBSTRING(@input,1,@ind-1)
                              SET @input = SUBSTRING(@input,@ind+1,LEN(@input)-@ind)
                              INSERT INTO @Result values (@str)
                              SET @ind = CharIndex(',',@input)
                        END
              SET @str = @input
              INSERT INTO @Result values (@str)
             END

          SET @Count = (SELECT Max(ID) from @Result)

             WHILE @Count > @max
              BEGIN
               SET @max = @max +1
               DECLARE @Value varchar(1000)
               SET @Value = (SELECT SUBSTRING(value, CHARINDEX('=', value) + 1, LEN(value)) from @Result WHERE ID =@max)
               SELECT @value
             END
    END

在最后一个while循环中,您可以根据需要分配相应的变量。

答案 2 :(得分:0)

尝试此查询:

`select wm_concat(substr(val,instr(val,'=')+1)) from (select  regexp_substr ('message_type=something, 
Hours=0, SQM=0, user_name=xxxxx, Boards=0, Charge=0, Proofs=0', '[^,]+', 1, rn) as val
 from dual
 cross
 join (select rownum as rn
         from dual
      connect by level <= 7));`