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

时间:2014-02-03 11:43:31

标签: sql sql-server

SQL查询:

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

但它显示错误:

  

'?'

附近的语法不正确

请帮帮我。

6 个答案:

答案 0 :(得分:10)

这适用于SQL Server。 IIF在SQL Server 2008或更早版本中不可用。

 SELECT * 
    FROM Account 
    WHERE 
    Id=IIF(type<> 100000002,"something",null )

如果您使用的是SQL Server 2008或更早版本,请尝试此操作。

  SELECT * 
    FROM Account 
    WHERE (Id= CASE WHEN type <>100000002 THEN 'something'  ELSE null END)

答案 1 :(得分:5)

你可以这样做:

SELECT * 
FROM Account 
where (type <>100000002 and Id='something') or (type =100000002 and id is null)

SELECT * 
FROM Account 
where  isnull(id,'_null_')= case when type <>100000002 then 'something' else  isnull(id,'_null_') end

答案 2 :(得分:2)

SELECT * 
FROM Account 
WHERE (Id= CASE WHEN type <>100000002 THEN 'something'  ELSE null END)

答案 3 :(得分:1)

使用以下内容:

SELECT * 
FROM Account 
WHERE (Id= CASE WHEN type != 100000002 THEN 'something'  ELSE null END)

SELECT * 
FROM Account 
WHERE (Id= CASE WHEN type <> 100000002 THEN 'something'  ELSE null END)

答案 4 :(得分:1)

SELECT * 
FROM Account 
where (type <>100000002 and Id='something') or (type =100000002)

答案 5 :(得分:0)