如何在 SQL Server 2008 中使用三元运算符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21526504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use ternary operator in SQL Server 2008?
提问by Sumon Banerjee
SQL query:
SQL查询:
SELECT *
FROM Account
WHERE (type <>100000002 ? Id='something': Id=null)
but it shows error :
但它显示错误:
Incorrect syntax near '?'
'?' 附近的语法不正确
Please help me.
请帮我。
回答by Sumon Banerjee
This is for SQL Server. IIF
is not available in SQL Server 2008 or earlier.
这是针对 SQL Server 的。IIF
在 SQL Server 2008 或更早版本中不可用。
SELECT *
FROM Account
WHERE
Id=IIF(type<> 100000002,"something",null )
If you are using SQL Server 2008 or earlier, then try this.
如果您使用的是 SQL Server 2008 或更早版本,请尝试此操作。
SELECT *
FROM Account
WHERE (Id= CASE WHEN type <>100000002 THEN 'something' ELSE null END)
回答by Royi Namir
You can do this :
你可以这样做 :
SELECT *
FROM Account
where (type <>100000002 and Id='something') or (type =100000002 and id is null)
or
或者
SELECT *
FROM Account
where isnull(id,'_null_')= case when type <>100000002 then 'something' else isnull(id,'_null_') end
回答by Krishnraj Rana
SELECT *
FROM Account
WHERE (Id= CASE WHEN type <>100000002 THEN 'something' ELSE null END)
回答by Developerzzz
Use the following:
使用以下内容:
SELECT *
FROM Account
WHERE (Id= CASE WHEN type != 100000002 THEN 'something' ELSE null END)
or
或者
SELECT *
FROM Account
WHERE (Id= CASE WHEN type <> 100000002 THEN 'something' ELSE null END)
回答by System
SELECT *
FROM Account
where (type <>100000002 and Id='something') or (type =100000002)
回答by Rahul
You can use case when...then in sql please see this link http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/
您可以使用 case when... then in sql 请参阅此链接 http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/