Oracle数据库的比较运算符Comparison Operators

news/2024/9/25 16:13:21/

Comparison operators compare one expression to another. The result is always either TRUE, FALSE, or NULL.

If the value of one expression is NULL, then the result of the comparison is also NULL.
如果一个表达式的值为NULL,那么比较的结果也是NULL。

The comparison operators are:

  • IS [NOT] NULL Operator
  • Relational Operators
  • LIKE Operator
  • BETWEEN Operator
  • IN Operator
CREATE OR REPLACE PROCEDURE print_boolean (b_name   VARCHAR2,b_value  BOOLEAN
) AUTHID DEFINER IS
BEGINIF b_value IS NULL THENDBMS_OUTPUT.PUT_LINE (b_name || ' = NULL');ELSIF b_value = TRUE THENDBMS_OUTPUT.PUT_LINE (b_name || ' = TRUE');ELSEDBMS_OUTPUT.PUT_LINE (b_name || ' = FALSE');END IF;
END;
/

1. IS [NOT] NULL Operator

The IS NULL operator returns the BOOLEAN value TRUE if its operand is NULL or FALSE if it is not NULL. The IS NOT NULL operator does the opposite.

Comparisons involving NULL values always yield NULL.

To test whether a value is NULL, use IF value IS NULL, as in these examples:

1.1 Variable Initialized to NULL by Default

In this example, the variable counter has the initial value NULL, by default. The example uses the “IS [NOT] NULL Operator” to show that NULL is different from zero.

DECLAREcounter INTEGER;  -- initial value is NULL by default
BEGINcounter := counter + 1;  -- NULL + 1 is still NULLIF counter IS NULL THENDBMS_OUTPUT.PUT_LINE('counter is NULL.');END IF;
END;
/
-- run result 
counter is NULL. 

1.2 Procedure Prints BOOLEAN Variable"

DECLAREPROCEDURE print_x_and_y (x  BOOLEAN,y  BOOLEAN) ISBEGINprint_boolean ('x', x);print_boolean ('y', y);print_boolean ('x AND y', x AND y);END print_x_and_y;BEGINprint_x_and_y (FALSE, FALSE);print_x_and_y (TRUE, FALSE);print_x_and_y (FALSE, TRUE);print_x_and_y (TRUE, TRUE);print_x_and_y (TRUE, NULL);print_x_and_y (FALSE, NULL);print_x_and_y (NULL, TRUE);print_x_and_y (NULL, FALSE);
END;
/
-- run result 
x = FALSE
y = FALSE
x AND y = FALSE
x = TRUE
y = FALSE
x AND y = FALSE
x = FALSE
y = TRUE
x AND y = FALSE
x = TRUE
y = TRUE
x AND y = TRUE
x = TRUE
y = NULL
x AND y = NULL
x = FALSE
y = NULL
x AND y = FALSE
x = NULL
y = TRUE
x AND y = NULL
x = NULL
y = FALSE
x AND y = FALSE

1.3 Searched CASE Expression with WHEN … IS NULL

DECLAREgrade CHAR(1); -- NULL by defaultappraisal VARCHAR2(20);
BEGINappraisal :=CASEWHEN grade IS NULL THEN 'No grade assigned'WHEN grade = 'A' THEN 'Excellent'WHEN grade = 'B' THEN 'Very Good'WHEN grade = 'C' THEN 'Good'WHEN grade = 'D' THEN 'Fair'WHEN grade = 'F' THEN 'Poor'ELSE 'No such grade'END;DBMS_OUTPUT.PUT_LINE ('Grade ' || grade || ' is ' || appraisal);
END;
/
-- run result
Grade  is No grade assigned 

2. Relational Operators

Operator Meaning

OperatorOperator
=equal to
<>, !=, ~=, ^=not equal to
<less than
>greater than
<=less than or equal to
>=greater than or equal to

Topics

  • Arithmetic Comparisons
  • BOOLEAN Comparisons
  • Character Comparisons
  • Date Comparisons

2.1 Relational Operators in Expressions

This example invokes the print_boolean procedure print the values of expressions that use relational operators to compare arithmetic values.

BEGINprint_boolean ('(2 + 2 =  4)', 2 + 2 = 4);print_boolean ('(2 + 2 <> 4)', 2 + 2 <> 4);print_boolean ('(2 + 2 != 4)', 2 + 2 != 4);print_boolean ('(2 + 2 ~= 4)', 2 + 2 ~= 4);print_boolean ('(2 + 2 ^= 4)', 2 + 2  ^= 4);print_boolean ('(1 < 2)', 1 < 2);print_boolean ('(1 > 2)', 1 > 2);print_boolean ('(1 <= 2)', 1 <= 2);print_boolean ('(1 >= 1)', 1 >= 1);
END;
/
-- run result
BEGINprint_boolean ('(2 + 2 =  4)', 2 + 2 = 4);print_boolean ('(2 + 2 <> 4)', 2 + 2 <> 4);print_boolean ('(2 + 2 != 4)', 2 + 2 != 4);print_boolean ('(2 + 2 ~= 4)', 2 + 2 ~= 4);print_boolean ('(2 + 2 ^= 4)', 2 + 2  ^= 4);print_boolean ('(1 < 2)', 1 < 2);print_boolean ('(1 > 2)', 1 > 2);print_boolean ('(1 <= 2)', 1 <= 2);print_boolean ('(1 >= 1)', 1 >= 1);
END;
/

2.2 BOOLEAN Comparisons

By definition, TRUE is greater than FALSE. Any comparison with NULL returns NULL.

2.3 Character Comparisons

By default, one character is greater than another if its binary value is larger.

For example, this expression is true:

'y' > 'r'

Strings are compared character by character. For example, this expression is true:

'Kathy' > 'Kathryn'

2.4 Date Comparisons

One date is greater than another if it is more recent.

For example, this expression is true:

'01-JAN-91' > '31-DEC-90'

3. LIKE Operator

LIKE Operator
The LIKE operator compares a character, string, or CLOB value to a pattern and returns TRUE if the value matches the pattern and FALSE if it does not.

Case is significant.

The pattern can include the two wildcard characters underscore (_) and percent sign (%).

Underscore matches exactly one character.

Percent sign (%) matches zero or more characters.

To search for the percent sign or underscore, define an escape character and put it before the percent sign or underscore.

3.1 LIKE Operator in Expression

The string ‘Johnson’ matches the pattern ‘J%s_n’ but not ‘J%S_N’, as this example shows.

DECLAREPROCEDURE compare (value   VARCHAR2,pattern VARCHAR2) ISBEGINIF value LIKE pattern THENDBMS_OUTPUT.PUT_LINE ('TRUE');ELSEDBMS_OUTPUT.PUT_LINE ('FALSE');END IF;END;
BEGINcompare('Johnson', 'J%s_n');compare('Johnson', 'J%S_N');
END;
/
-- run result
TRUE
FALSE 

4. BETWEEN Operator

The BETWEEN operator tests whether a value lies in a specified range.

The value of the expression x BETWEEN a AND b is defined to be the same as the value of the expression (x>=a) AND (x<=b) . The expression x will only be evaluated once.

4.1 BETWEEN Operator in Expressions

This example invokes the print_boolean procedure print the values of expressions that include the BETWEEN operator.

BEGINprint_boolean ('2 BETWEEN 1 AND 3', 2 BETWEEN 1 AND 3);print_boolean ('2 BETWEEN 2 AND 3', 2 BETWEEN 2 AND 3);print_boolean ('2 BETWEEN 1 AND 2', 2 BETWEEN 1 AND 2);print_boolean ('2 BETWEEN 3 AND 4', 2 BETWEEN 3 AND 4);
END;
/
-- run result
2 BETWEEN 1 AND 3 = TRUE
2 BETWEEN 2 AND 3 = TRUE
2 BETWEEN 1 AND 2 = TRUE
2 BETWEEN 3 AND 4 = FALSEPL/SQL procedure successfully completed.

5. IN Operator

The IN operator tests set membership.

x IN (set) returns TRUE only if x equals a member of set.

5.1 IN Operator in Expressions

This example invokes the print_boolean procedure print the values of expressions that include the IN operator.

DECLAREletter VARCHAR2(1) := 'm';
BEGINprint_boolean ('letter IN (''a'', ''b'', ''c'')',letter IN ('a', 'b', 'c'));print_boolean ('letter IN (''z'', ''m'', ''y'', ''p'')',letter IN ('z', 'm', 'y', 'p'));
END;
/
-- run result
letter IN ('a', 'b', 'c') = FALSE
letter IN ('z', 'm', 'y', 'p') = TRUEPL/SQL procedure successfully completed.

5.2 IN Operator with Sets with NULL Values

This example shows what happens when set includes a NULL value. This invokes the print_boolean procedure

DECLAREa INTEGER; -- Initialized to NULL by defaultb INTEGER := 10;c INTEGER := 100;
BEGINprint_boolean ('100 IN (a, b, c)', 100 IN (a, b, c));print_boolean ('100 NOT IN (a, b, c)', 100 NOT IN (a, b, c));print_boolean ('100 IN (a, b)', 100 IN (a, b));print_boolean ('100 NOT IN (a, b)', 100 NOT IN (a, b));print_boolean ('a IN (a, b)', a IN (a, b));print_boolean ('a NOT IN (a, b)', a NOT IN (a, b));
END;
/
-- run result
100 IN (a, b, c) = TRUE
100 NOT IN (a, b, c) = FALSE
100 IN (a, b) = NULL
100 NOT IN (a, b) = NULL
a IN (a, b) = NULL
a NOT IN (a, b) = NULLPL/SQL procedure successfully completed. 

http://www.ppmy.cn/news/1530361.html

相关文章

cmaklist流程控制——调试及发布

cmaklist流程控制 目前只会配置-编译调试-打包发布&#xff0c;并且不会workflow控制 后续学习配置-编译调试-测试-打包发布&#xff0c;workflow控制&#xff0c;理解整个流程&#xff0c;目前对流程控制理解也不够。 1.CMake Presets 先于Cmakelist文件&#xff0c;指导项…

14、线程池ForkJoinPool实战及其工作原理分析

1. 由一道算法题引发的思考 算法题&#xff1a;如何充分利用多核CPU的性能&#xff0c;快速对一个2千万大小的数组进行排序&#xff1f; 1&#xff09;首先这是一道排序的算法题&#xff0c;而且是需要使用高效的排序算法对2千万大小的数组进行排序&#xff0c;可以考虑使用快…

web平台搭建-LAMP(CentOS-7)

一. 准备工作 环境要求&#xff1a; 操作系统&#xff1a;CentOS 7.X 64位 网络配置&#xff1a;nmtui字符终端图形管理工具或者直接编辑配置文件 关闭SELinux和firewalld防火墙 防火墙&#xff1a; 临时关闭&#xff1a;systemctl stop firewalld 永久关闭&#xff1a;systemc…

React——点击事件函数调用问题

问题 <MessageOutlined onClick{handleIconClick(test_task_id,test_run_id)} style{{ width: 36 ,color: #3875f6, filter: brightness(1.5)}} />直接在onClick属性中调用函数并传递参数的语法会有问题。 在JSX中如果想要在事件处理器&#xff08;如onClick&#xff…

Git clone远程仓库没有其他分支的问题

在使用Git克隆&#xff08;Git clone&#xff09;时&#xff0c;可能遇到分支不全的问题。有以下几种可能的原因和解决方法&#xff1a; 未将所有分支克隆下来&#xff1a;默认情况下&#xff0c;Git只会克隆远程仓库的主分支。如果您想要克隆其他分支&#xff0c;可以使用以下…

uniapp组件封装和父子组件间通讯的介绍和详细案例

Uniapp 是一个使用 Vue.js 开发跨平台应用的前端框架&#xff0c;组件封装和父子组件间通讯是其重要的特性之一。 一、组件封装 为什么要封装组件 提高代码的可维护性和可复用性。将特定功能封装成独立的组件&#xff0c;可以在不同的页面或项目中重复使用&#xff0c;减少代码…

让C#程序在linux环境运行

今晚花一些时间&#xff0c;总结net程序如何在linux环境运行的一些技术路线。 1、采用.Net Core框架 NET Core 使用了 .NET Core Runtime&#xff0c;它可以在 Windows、Linux 和 macOS 等多个操作系统上运行。可以采用Visual Studio生成Linux版本的dll。 在Linux系统中&…

C++的哲学思想

C的哲学思想 文章目录 C的哲学思想&#x1f4a1;前言&#x1f4a1;C的哲学思想☁️C底层不应该基于任何其他语言&#xff08;汇编语言除外&#xff09;☁️只为使用的东西付费&#xff08;不需要为没有使用到的语言特性付费&#xff09;☁️以低成本提供高级抽象&#xff08;更…