简体   繁体   English

如何在SQL Server 2008中使用三元运算符?

[英]How to use ternary operator in SQL Server 2008?

SQL query: SQL查询:

SELECT * 
FROM Account 
WHERE (type <>100000002 ? Id='something': Id=null)

but it shows error : 但它显示错误:

Incorrect syntax near '?' “?”附近的语法不正确

Please help me. 请帮我。

This is for SQL Server. 这适用于SQL Server。 IIF is not available in SQL Server 2008 or earlier. 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)

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
SELECT * 
FROM Account 
WHERE (Id= CASE WHEN type <>100000002 THEN 'something'  ELSE null END)

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)
SELECT * 
FROM Account 
where (type <>100000002 and Id='something') or (type =100000002)

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

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