📜  DocumentDB SQL-标量表达式(1)

📅  最后修改于: 2023-12-03 15:00:31.339000             🧑  作者: Mango

DocumentDB SQL Scalar Expressions

Scalar expressions in DocumentDB SQL refer to a single value expression that returns a scalar value. In simpler words, it is an expression that returns a result of a single value, unlike a table expression that returns a set of values. Scalar expressions can be used in various ways in DocumentDB SQL queries.

The following are some examples of scalar expressions in DocumentDB SQL:

  • Arithmetic expressions: Used for performing mathematical operations on numeric values. Examples include addition, subtraction, multiplication, and division.
SELECT 4 + 5            -- returns 9
SELECT 4 - 5            -- returns -1
SELECT 4 * 5            -- returns 20
SELECT 4 / 5            -- returns 0.8
  • Comparison expressions: Used for comparing values. Examples include greater than (>), less than (<), equal to (=), and not equal to (!=).
SELECT 4 > 5            -- returns false
SELECT 4 < 5            -- returns true
SELECT 4 = 5            -- returns false
SELECT 4 != 5           -- returns true
  • Logical expressions: Used for logical operations. Examples include AND, OR, and NOT.
SELECT NOT(4 > 5)       -- returns true
SELECT (4 > 5) OR (4 < 5) -- returns true
SELECT (4 > 5) AND (4 < 5) -- returns false
  • Function expressions: Used for performing predefined functions on values. Examples include ABS (returns the absolute value of a numeric value), LOWER (returns the lowercase version of a string), and UPPER (returns the uppercase version of a string).
SELECT ABS(-4)          -- returns 4
SELECT LOWER('HELLO')   -- returns 'hello'
SELECT UPPER('world')   -- returns 'WORLD'
  • Constant expressions: Refers to a constant value. Examples include integer values, string values, and Boolean values.
SELECT 4                -- returns 4
SELECT 'hello'          -- returns 'hello'
SELECT true             -- returns true

Scalar expressions can be used in various parts of a DocumentDB SQL query like SELECT statements, WHERE clauses, ORDER BY clauses, etc. These expressions help in performing complex calculations, filtering data, and sorting results.

In conclusion, scalar expressions are a powerful and essential feature of DocumentDB SQL. They provide a concise and efficient way to perform arithmetic, comparisons, logical, and function operations on values. Programmers can leverage scalar expressions to build robust and scalable DocumentDB SQL queries.