如何在查询语句里对字段进行判断?

Dhammacakkhu 2003-12-12 03:55:23
如SELECT field1,iif(field2>1,true,false) FROM table1

有这样的语法吗?
...全文
154 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
KK4 2003-12-12
  • 打赏
  • 举报
回复
SELECT field1,case when field2>1 then 'true' else 'false' end FROM table1
dlpseeyou 2003-12-12
  • 打赏
  • 举报
回复
CASE
计算条件列表并返回多个可能结果表达式之一。

CASE 具有两种格式:

简单 CASE 函数将某个表达式与一组简单表达式进行比较以确定结果。


CASE 搜索函数计算一组布尔表达式以确定结果。
两种格式都支持可选的 ELSE 参数。

语法
简单 CASE 函数:

CASE input_expression
WHEN when_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
END

CASE 搜索函数:

CASE
WHEN Boolean_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
END

参数
input_expression

是使用简单 CASE 格式时所计算的表达式。Input_expression 是任何有效的 Microsoft® SQL Server™ 表达式。

WHEN when_expression

使用简单 CASE 格式时 input_expression 所比较的简单表达式。When_expression 是任意有效的 SQL Server 表达式。Input_expression 和每个 when_expression 的数据类型必须相同,或者是隐性转换。

n

占位符,表明可以使用多个 WHEN when_expression THEN result_expression 子句或 WHEN Boolean_expression THEN result_expression 子句。

THEN result_expression

当 input_expression = when_expression 取值为 TRUE,或者 Boolean_expression 取值为 TRUE 时返回的表达式。result expression 是任意有效的 SQL Server 表达式。

ELSE else_result_expression

当比较运算取值不为 TRUE 时返回的表达式。如果省略此参数并且比较运算取值不为 TRUE,CASE 将返回 NULL 值。Else_result_expression 是任意有效的 SQL Server 表达式。Else_result_expression 和所有 result_expression 的数据类型必须相同,或者必须是隐性转换。

WHEN Boolean_expression

使用 CASE 搜索格式时所计算的布尔表达式。Boolean_expression 是任意有效的布尔表达式。

结果类型
从 result_expressions 和可选 else_result_expression 的类型集合中返回最高的优先规则类型。有关更多信息,请参见数据类型的优先顺序。

结果值
zjcxc 元老 2003-12-12
  • 打赏
  • 举报
回复
--ACCESS中就没错,SQL中用case when
SELECT field1,case when field2>1 then 'true' else 'false' end FROM table1
victorycyz 2003-12-12
  • 打赏
  • 举报
回复
Access有可以用iif(),SQL Server中要用case语句替代。
Rotaxe 2003-12-12
  • 打赏
  • 举报
回复
select field1,case when field2>1 then 'true' else 'false' end FROM table1
devilwind 2003-12-12
  • 打赏
  • 举报
回复
select field1,field2=case when field2>2 then 'true' else 'false' end from table1
Rotaxe 2003-12-12
  • 打赏
  • 举报
回复
没有,可以用
select field1,case when field2>1 then true else false end FROM table1

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧