简体   繁体   English

SQL if / case语句-从数据库返回值

[英]SQL if/case statement - returning value from db

I have trouble with sql command in MS Sql server ..... I would like to check names in table. 我在MS Sql服务器中使用sql命令遇到麻烦.....我想检查表中的名称。 in Column are eg Carl, Loius, Manfred, Jenny .... Is there any way, to create case or if statement : 在列中有例如Carl,Loius,Manfred,Jenny...。有什么方法可以创建案例或if语句:

Case  when name = 'Carl' then 'Man'
else Return original value(name) from table?

(Louis, Manfred, Jenny) ? (路易斯,曼弗雷德,珍妮)?

If yes, how can i achieve that ? 如果是,我该如何实现? Case statement? 案件陈述? If statement? 如果声明? Thanks in advance 提前致谢

你可以做:

 select (Case when name = 'Carl' then 'Man' else name end)

you mean something like this ? 你的意思是这样的吗?

select name,
       case when name = 'Carl' then 'Man' 
            when name = 'Jenny' then 'women'
       else name end
from   yourtable
  Select case when name in ('Carl','Manfred',....) 
   Then 'Man' else 'Women' end as gender 
    from your_table

Another (shorter to write) option is to use nullif and isnull: 另一个(较短的编写时间)选项是使用nullif和isull:

SELECT ISNULL(NULLIF(name, 'Carl'), name)) as name
FROM yourTable.

The NULLIF function will return null if both arguments are equale, and the ISNULL will return the first argument unless it's null - in that case it will return the second argument. 如果两个参数相等, NULLIF函数将返回null,并且ISNULL将返回第一个参数(除非它为null)-在这种情况下,它将返回第二个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM