首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何根据Server 2008中的特定参数返回所需的手动值

如何根据Server 2008中的特定参数返回所需的手动值
EN

Stack Overflow用户
提问于 2016-06-01 18:05:40
回答 3查看 132关注 0票数 3

Server 2008中有一个SQL查询,如下所示:

代码语言:javascript
运行
复制
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作为一个字符串(而不是不同的行)

我怎么能拿到这个?如果你能帮忙我很感激。

谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-06-01 18:14:36

代码语言:javascript
运行
复制
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,'')
票数 3
EN

Stack Overflow用户

发布于 2016-06-01 18:18:50

我想如果有几个街区的话你可能会得到更好的服务。

代码语言:javascript
运行
复制
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);
票数 0
EN

Stack Overflow用户

发布于 2016-06-01 18:31:07

在这里,您已经采取了这样的情况,您可以通过只执行连接,

代码语言:javascript
运行
复制
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值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37575848

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档