用分隔符分隔字符串并赋值给变量-sql

时间:2018-04-13 05:53:10

标签: sql sql-server delimiter

我有字符串'sunday,monday,thursday'我想将它们分开并将它们分配给@day1, @day2, @day3之类的变量。刺痛的某些天数会有所不同。如何在sql中实现这一点?

2 个答案:

答案 0 :(得分:0)

尝试以下

DECLARE
  @day1 varchar(10),
  @day2 varchar(10),
  @day3 varchar(10),
  @day4 varchar(10)

DECLARE @Split TABLE (N int IDENTITY, S varchar(200))

INSERT @Split (S) SELECT value FROM STRING_SPLIT('sunday,monday,thursday',',')

SELECT
  @day1=MAX(CASE WHEN N=1 THEN S END),
  @day2=MAX(CASE WHEN N=2 THEN S END),
  @day3=MAX(CASE WHEN N=3 THEN S END),
  @day4=MAX(CASE WHEN N=4 THEN S END) -- is null
FROM @Split

-- check
SELECT @day1,@day2,@day3,@day4

具有CTE的变体

DECLARE
  @day1 varchar(10),
  @day2 varchar(10),
  @day3 varchar(10),
  @day4 varchar(10),
  @day5 varchar(10),
  @day6 varchar(10),
  @day7 varchar(10)

DECLARE @input varchar(100)='sunday,monday,thursday'

;WITH sCTE AS(
  SELECT
    1 n,
    NULLIF(LEFT(@input,ISNULL(NULLIF(CHARINDEX(',',@input)-1,-1),LEN(@input))),'') s,
    IIF(CHARINDEX(',',@input)=0,'',RIGHT(@input,LEN(@input)-CHARINDEX(',',@input))) p
  UNION ALL
  SELECT
    n+1,
    LEFT(p,ISNULL(NULLIF(CHARINDEX(',',p)-1,-1),LEN(p))) s,
    IIF(CHARINDEX(',',p)=0,'',RIGHT(p,LEN(p)-CHARINDEX(',',p))) p
  FROM sCTE
  WHERE p<>''
)
SELECT
  @day1=MAX(CASE WHEN N=1 THEN S END),
  @day2=MAX(CASE WHEN N=2 THEN S END),
  @day3=MAX(CASE WHEN N=3 THEN S END),
  @day4=MAX(CASE WHEN N=4 THEN S END),
  @day5=MAX(CASE WHEN N=4 THEN S END),
  @day6=MAX(CASE WHEN N=4 THEN S END),
  @day7=MAX(CASE WHEN N=4 THEN S END)
FROM sCTE

-- check
SELECT @day1,@day2,@day3,@day4,@day5,@day6,@day7

如果您有monday,tuesday,friday之类的输入字符串,并且想要将其设置为@day2=monday; @day3=tuesday; @day6=friday

,则为变体
DECLARE
  @day1 varchar(10),
  @day2 varchar(10),
  @day3 varchar(10),
  @day4 varchar(10),
  @day5 varchar(10),
  @day6 varchar(10),
  @day7 varchar(10)

DECLARE @input varchar(100)='monday,tuesday,friday'

SET @day1=IIF(CHARINDEX('sunday',@input)>0,'sunday',NULL)
SET @day2=IIF(CHARINDEX('monday',@input)>0,'monday',NULL)
SET @day3=IIF(CHARINDEX('tuesday',@input)>0,'tuesday',NULL)
SET @day4=IIF(CHARINDEX('wednesday',@input)>0,'wednesday',NULL)
SET @day5=IIF(CHARINDEX('thursday',@input)>0,'thursday',NULL)
SET @day6=IIF(CHARINDEX('friday',@input)>0,'friday',NULL)
SET @day7=IIF(CHARINDEX('saturday',@input)>0,'saturday',NULL)

-- check
SELECT @day1,@day2,@day3,@day4,@day5,@day6,@day7

答案 1 :(得分:0)

创建了一个动态sql查询。不知道它有多高效。

<强>查询

declare @days as varchar(max) = 'sunday,monday,thursday';

declare @t as table([id] int not null identity(1, 1), [value] varchar(100));
insert into @t([value])
select [Value] from String_Split(@days, ',');

declare @i as int;
select @i = count(*) from @t;

declare @j as int;
set @j = 1;

declare @sql1 as varchar(max) = '';
declare @sql2 as varchar(max) = '';

while(@j <= @i)
begin
  select @sql1 += 'declare @day' + cast(@j as varchar(10)) + ' as varchar(100);'
               +  'set @day' + cast(@j as varchar(10)) + ' = '
               + char(39)
               + (select [value] from @t where [id] = @j) 
               + char(39)
               + ';';

  select @sql2 += '@day' + cast(@j as varchar(10)) 
               + ' as [@day' + cast(@j as varchar(10)) + '] ,';          

  set @j += 1;
end

select @sql2 = 'select ' + left(@sql2, len(@sql2) - 1);

declare @sql as varchar(max) = @sql1 + @sql2;

exec(@sql);

<强> Find a demo here