Server 2008中有一个SQL查询,如下所示:
declare @Waited6_8 varchar(max) = 'true'
declare @Waited8_12 varchar(max) = 'false'
declare @Waited12_18 varchar(max) = 'true'
Select
choice = case when @Waited6_8 = 'true' then '6-8'
when @Waited8_12 = 'true' then '8-12'
when @Waited12_18 = 'true' then '12-18'
end
在这里,我得到了6-8
作为结果。
我想看到的是:6-8, 12-18
作为一个字符串(而不是不同的行)
我怎么能拿到这个?如果你能帮忙我很感激。
谢谢!
发布于 2016-06-01 18:14:36
declare @Waited6_8 varchar(max) = 'true'
declare @Waited8_12 varchar(max) = 'false'
declare @Waited12_18 varchar(max) = 'true'
Select choice = isnull(case when @Waited6_8 = 'true' then '6-8' end + ', ','') +
isnull(case when @Waited8_12 = 'true' then '8-12' end + ', ','') +
isnull(case when @Waited12_18 = 'true' then '12-18' end,'')
发布于 2016-06-01 18:18:50
我想如果有几个街区的话你可能会得到更好的服务。
DECLARE @Waited6_8 varchar(max) = 'true',
@Waited8_12 varchar(max) = 'false',
@Waited12_18 varchar(max) = 'true',
@Choice VARCHAR(MAX)
IF (@Waited6_8 = 'true')
BEGIN
SET @Choice += '6-8 ';
END
IF (@Waited8_12 = 'true')
BEGIN
SET @Choice += '8-12 ';
END
IF (Waited12_18 = 'true')
BEGIN
SET @Choice += '12-18';
END
PRINT RTRIM(@Choice);
发布于 2016-06-01 18:31:07
在这里,您已经采取了这样的情况,您可以通过只执行连接,
declare @Waited6_8 varchar(max) = 'true'
declare @Waited8_12 varchar(max) = 'false'
declare @Waited12_18 varchar(max) = 'true'
Select choice = isnull(case when @Waited6_8 = 'true' then '6-8' end + ', ','') +
isnull(case when @Waited8_12 = 'true' then '8-12' end + ', ','') + //This case will get omitted as it contains null value
isnull(case when @Waited12_18 = 'true' then '12-18' end,'')
检查输出,
请参考工作小提琴,http://sqlfiddle.com/#!6/9eecb7/8083,我已经创建了这个例子,关于您的question.this可能解决您的所有问题。
在这里,它连接了所有3种情况,但是忽略了空值情况,所以您将不会得到中间情况的值。
请注意,请确保将ISNULL放在每种情况之前,它还将显示中间大小写的null值。
https://stackoverflow.com/questions/37575848
复制相似问题