需要将变量拆分为6个变量 - SQL

时间:2016-09-21 16:03:23

标签: sql tsql variables substring charindex

需要帮助填补空白我正在努力将变量分成6个变量。

   DECLARE @item VARCHAR(MAX) = 'MG1111.TG2222.MW3333.JG4444.MG5555.MH6666'
   DECLARE @item1 VARCHAR(MAX) 
   DECLARE @item2 VARCHAR(MAX) 
   DECLARE @item3 VARCHAR(MAX) 
   DECLARE @item4 VARCHAR(MAX) 
   DECLARE @item5 VARCHAR(MAX) 
   DECLARE @item6 VARCHAR(MAX) 

  set @item1 = (SUBSTRING( @item, 0, CHARINDEX('.', @item)))

  set @item2 = (SUBSTRING(SUBSTRING( @item, CHARINDEX('.', @item)+1,LEN(@ITEM)),0,CHARINDEX('.', SUBSTRING( @item, CHARINDEX('.', @item)+1,LEN(@ITEM))) ))

  set @item6 = (REVERSE(SUBSTRING( REVERSE(@ITEM), 0, CHARINDEX('.' , REVERSE(@ITEM)))))

  print @item1
  print @item2
  print @item3
  print @item4
  print @item5
  print @item6

4 个答案:

答案 0 :(得分:0)

为什么不使用这种模式?

set @item1 = SUBSTRING_INDEX( @item, '.', 1)
.
set @item3 = SUBSTRING_INDEX( SUBSTRING_INDEX( @item, '.', 2), '.', 1)
.
.
set @item6 = SUBSTRING_INDEX( @item, '.', -1)

我假设有6个变量,你的意思是@item中点之间的每个子串都是一个单独的"变量"。

答案 1 :(得分:0)

使用XML技巧

这是一种更简单的方法
DECLARE @item VARCHAR(max) = 'MG1111.TG2222.MW3333.JG4444.MG5555.MH6666' 
DECLARE @item1 VARCHAR(max) 
DECLARE @item2 VARCHAR(max) 
DECLARE @item3 VARCHAR(max) 
DECLARE @item4 VARCHAR(max) 
DECLARE @item5 VARCHAR(max) 
DECLARE @item6 VARCHAR(max); 

WITH split_names 
     AS (SELECT CONVERT(XML, '<Names><name>' + Replace(@item, '.', '</name><name>') + '</name></Names>') AS xmlname) 
SELECT @item1 = xmlname.value('/Names[1]/name[1]', 'varchar(50)'), 
       @item2 = xmlname.value('/Names[1]/name[2]', 'varchar(50)'), 
       @item3 = xmlname.value('/Names[1]/name[3]', 'varchar(50)'), 
       @item4 = xmlname.value('/Names[1]/name[4]', 'varchar(50)'), 
       @item5 = xmlname.value('/Names[1]/name[5]', 'varchar(50)'), 
       @item6 = xmlname.value('/Names[1]/name[6]', 'varchar(50)') 
FROM   split_names 

答案 2 :(得分:0)

几乎任何String Parser都可以。也就是说,我有一个会返回(当前)最多9个变量

Select @item1=Pos1
      ,@item2=Pos2
      ,@item3=Pos3
      ,@item4=Pos4
      ,@item5=Pos5
      ,@item6=Pos6
 From [dbo].[udf-Str-Parse-Row](@Item,'.')

UDF

CREATE FUNCTION [dbo].[udf-Str-Parse-Row] (@String varchar(max),@Delimeter varchar(10))
--Usage: Select * from [dbo].[udf-Str-Parse-Row]('Dog,Cat,House,Car',',')
--       Select * from [dbo].[udf-Str-Parse-Row]('John Cappelletti',' ')
--       Select * from [dbo].[udf-Str-Parse-Row]('id26,id46|id658,id967','|')

Returns Table 
As
Return (
    SELECT Pos1 = xDim.value('/x[1]','varchar(250)')
          ,Pos2 = xDim.value('/x[2]','varchar(250)')
          ,Pos3 = xDim.value('/x[3]','varchar(250)')
          ,Pos4 = xDim.value('/x[4]','varchar(250)')
          ,Pos5 = xDim.value('/x[5]','varchar(250)')
          ,Pos6 = xDim.value('/x[6]','varchar(250)')
          ,Pos7 = xDim.value('/x[7]','varchar(250)')
          ,Pos8 = xDim.value('/x[8]','varchar(250)')
          ,Pos9 = xDim.value('/x[9]','varchar(250)')
    FROM (Select Cast('<x>' + Replace(@String,@Delimeter,'</x><x>')+'</x>' as XML) as xDim) A
)

答案 3 :(得分:0)

更通用的解析器可能如下所示。这个返回一个行号,并不限于深度。

DECLARE @item VARCHAR(MAX) = 'MG1111.TG2222.MW3333.JG4444.MG5555.MH6666'
DECLARE @item1 VARCHAR(MAX) 
DECLARE @item2 VARCHAR(MAX) 
DECLARE @item3 VARCHAR(MAX) 
DECLARE @item4 VARCHAR(MAX) 
DECLARE @item5 VARCHAR(MAX) 
DECLARE @item6 VARCHAR(MAX) 

Select @item1=max(case when Key_PS=1 then Key_Value else '' end)
      ,@item2=max(case when Key_PS=2 then Key_Value else '' end)
      ,@item3=max(case when Key_PS=3 then Key_Value else '' end)
      ,@item4=max(case when Key_PS=4 then Key_Value else '' end)
      ,@item5=max(case when Key_PS=5 then Key_Value else '' end)
      ,@item6=max(case when Key_PS=6 then Key_Value else '' end)
  From [dbo].[udf-Str-Parse](@Item,'.')

UDF

CREATE FUNCTION [dbo].[udf-Str-Parse] (@String varchar(max),@Delimeter varchar(10))
--Usage: Select * from [dbo].[udf-Str-Parse]('Dog,Cat,House,Car',',')
--       Select * from [dbo].[udf-Str-Parse]('John Cappelletti was here',' ')

Returns @ReturnTable Table (Key_PS int IDENTITY(1,1), Key_Value varchar(max))
As
Begin
   Declare @XML xml;Set @XML = Cast('<x>' + Replace(@String,@Delimeter,'</x><x>')+'</x>' as XML)
   Insert Into @ReturnTable Select ltrim(rtrim(String.value('.', 'varchar(max)'))) FROM @XML.nodes('x') as T(String)
   Return 
End