Sql server在when语句中显示字符串(Sql server display string in when statement)

编程入门 行业动态 更新时间:2024-10-26 06:35:23
Sql server在when语句中显示字符串(Sql server display string in when statement)

我有以下查询

select (case when object like'%service%' then (object) when class like '%service%' then (class)end) From db group by (case when object like '%service%' then object when object_class like '%service%' then object_class end)

此查询检查每列的值是否包含'service'而不是显示数据,我需要代替仅显示数据,数据和字符串 有点when object like'%service%' then (object),'string'

String将是一个变量,在结果中存储在未知列中

column1 column2 objet1 mystring objet2 mystring objet3 mystring

I have the following query

select (case when object like'%service%' then (object) when class like '%service%' then (class)end) From db group by (case when object like '%service%' then object when object_class like '%service%' then object_class end)

this query check if value of each column contains 'service' than display the data, i need instead of data only display, data and string Some thing like when object like'%service%' then (object),'string'

String will be a variable which is stored in unknown column at results

column1 column2 objet1 mystring objet2 mystring objet3 mystring

最满意答案

CASE语句只返回一个值 - 您不能使用它返回多个列。

您可以在每列中重复case语句:

select case when object like '%service%' then object when class like '%service%' then class end as column1, case when object like '%service%' then 'string' when class like '%service%' then 'string' end as column2 FROM ...

或做一个UNION :

select object as column1, 'string' as column2 WHERE object like '%service%' FROM ... UNION ALL select class as column1, 'string' as column2 WHERE class like '%service%' FROM ...

请注意,如果有任何记录,其中object和class 包含字符串service ,则结果将不同。 UNION将返回两条记录, CASE将只包含一条(首先匹配object )。

The CASE statement only returns one value - you cannot use it to return multiple columns.

You can either repeat the case statement in each column:

select case when object like '%service%' then object when class like '%service%' then class end as column1, case when object like '%service%' then 'string' when class like '%service%' then 'string' end as column2 FROM ...

or do a UNION:

select object as column1, 'string' as column2 WHERE object like '%service%' FROM ... UNION ALL select class as column1, 'string' as column2 WHERE class like '%service%' FROM ...

Note that the results will be different if there are any records where object and class both contain the string service. The UNION will return two records, the CASE will only include one (matching on object first).

更多推荐

本文发布于:2023-04-27 20:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329054.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   语句   server   Sql   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!