

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 标量函数
<a name="sql-functions-scalar"></a>

 标量函数接受一个或多个输入值并返回单个输出值。它们在 SQL（结构化查询语言）中被广泛用于数据操作和检索，从而提高了数据处理任务的效率。

**Topics**
+ [空数据函数](sql-functions-null.md)
+ [字符串函数](sql-functions-string.md)
+ [数学函数](sql-functions-math.md)
+ [日期时间函数](sql-functions-date.md)
+ [类型转换函数](sql-functions-type-conv.md)

# 空数据函数
<a name="sql-functions-null"></a>

 Null 数据函数处理或操作 NULL 值，这些值表示缺少值。这些函数允许您 NULLs 用其他值替换、检查值是否为 NULL 或执行以特定 NULLs 方式处理的操作。


|  **函数**  |  **签名**  |  **描述**  | 
| --- | --- | --- | 
|  `COALESCE`  |   COALESCE（表达式 1、表达式 2、...、expressionN）   |  如果所有表达式的计算结果均为空，则 COALESCE 返回空值。表达式的类型必须相同。  | 

**Example COALESCE 函数的**  

```
SELECT COALESCE (l.double_value, 100) AS non_double_value FROM latest_value_time_series AS l LIMIT 1
```

# 字符串函数
<a name="sql-functions-string"></a>

 字符串函数是用于操作和处理文本数据的内置工具。它们支持串联、提取、格式化和在字符串中搜索等任务。这些功能对于清理、转换和分析数据库中基于文本的数据至关重要。


**字符串函数**  

|  **函数**  |  **签名**  |  **描述**  | 
| --- | --- | --- | 
|  `LENGTH`  |   长度（字符串）   |  返回字符串的长度。  | 
|  `CONCAT`  |   CONCAT（字符串、字符串）   |  连接字符串中的参数。  | 
|  `SUBSTR`  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/iot-sitewise/latest/userguide/sql-functions-string.html)  |  返回以下内容之一： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/iot-sitewise/latest/userguide/sql-functions-string.html) 使用基于 1 的索引作为起始参数。  | 
|  `UPPER`  |   UPPER（字符串）   |  将输入字符串中的字符转换为大写。  | 
|  `LOWER`  |   下限（字符串）   |  将输入字符串中的字符转换为小写。  | 
|  `TRIM`  |   修剪（字符串）   |  删除字符串开头、结尾或两边的所有空格字符。  | 
|  `LTRIM`  |   LTRIM（字符串）   |  删除字符串开头的所有空格字符。  | 
|  `RTRIM`  |   RTRIM（字符串）   |  删除字符串末尾的所有空格字符。  | 
|  `STR_REPLACE`  |   STR\$1REPLACE（字符串，从、到）   |  用另一个指定的子字符串替换所有出现的指定子字符串。  | 

所有功能的示例：


|  **函数**  |  **示例**  | 
| --- | --- | 
|  LENGTH  |  `SELECT LENGTH(a.asset_id) AS asset_id_length FROM asset AS a`  | 
|  CONCAT  |   `SELECT CONCAT(p.property_id, p.property_name) FROM asset_property AS p`   | 
|  SUBSTR  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/iot-sitewise/latest/userguide/sql-functions-string.html)  | 
|  UPPER  |   `SELECT UPPER(d.string_value) AS up_string FROM raw_time_series AS d`   | 
|  LOWER  |   `SELECT LOWER(d.string_value) AS low_string FROM raw_time_series AS d`   | 
|  TRIM  |   `SELECT TRIM(d.string_value) AS tm_string FROM raw_time_series AS d`   | 
|  LTRIM  |   `SELECT LTRIM(d.string_value) AS ltrim_string FROM raw_time_series AS d`   | 
|  RTRIM  |   `SELECT RTRIM(d.string_value) AS rtrim_string FROM raw_time_series AS d`   | 
|  STR\$1REPALE  |   `SELECT STR_REPLACE(d.string_value, 'abc', 'def') AS replaced_string FROM raw_time_series AS d`   | 

## 串联运算符
<a name="sql-operators-concatenation"></a>

 连接运算符或管道运算`||`符将两个字符串连接在一起。它提供了该`CONCAT`函数的替代方案，并且在组合多个字符串时更具可读性。

**Example 的串联运算符**  

```
SELECT a.asset_name || ' - ' || p.property_name 
  AS full_name
  FROM asset a, asset_property p
```

# 数学函数
<a name="sql-functions-math"></a>

 数学函数是预定义的数学运算，用于在 SQL 查询中对数值数据进行计算。它们提供了操作和转换数据的方法，而无需从数据库中提取数据并单独处理数据。


**数学函数**  

|  **函数**  |  **签名**  |  **描述**  | 
| --- | --- | --- | 
|  `POWER`  |  功率（整数\$1双精度，整数\$1双精度）  |  返回第一个参数的值乘以第二个参数的次方。  | 
|  `ROUND`  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/iot-sitewise/latest/userguide/sql-functions-math.html)  |  四舍五入到最接近的整数。  | 
|  `FLOOR`  |   地板 (int\$1double)   |  返回不大于给定值的最大整数。  | 

所有功能的示例：


|  **函数**  |  **示例**  | 
| --- | --- | 
|  POWER  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/iot-sitewise/latest/userguide/sql-functions-math.html)  | 
|  ROUND  |   `ROUND (32.12435, 3)`   | 
|  FLOOR  |   `FLOOR (21.2)`   | 

# 日期时间函数
<a name="sql-functions-date"></a>

 日期时间函数适用于日期和时间。这些函数允许提取日期的特定组成部分、执行计算和操作日期值。

这些函数中允许使用的标识符是：
+ YEAR
+ MONTH
+ DAY
+ HOUR
+ MINUTE
+ SECOND


|  **函数**  |  **签名**  |  **描述**  | 
| --- | --- | --- | 
|  `NOW`  |   现在 ()   |  返回精度为毫秒的当前时间戳。它提供了在查询中执行时的确切时间。  | 
|  `DATE_ADD`  |  DATE\$1ADD（标识符、间隔持续时间、列）  |  返回 a date/time 和多个days/hours, or of a date/time and date/time间隔的总和。  | 
|  `DATE_SUB`  |  DATE\$1SUB（标识符、间隔持续时间、列）  |  返回 a date/time 和多个间days/hours, or between a date/time and date/time隔之间的差。  | 
|  `TIMESTAMP_ADD`  |  TIMESTAMP\$1ADD（标识符、间隔持续时间、列）  |  在日期时间表达式中添加以给定时间单位为单位的时间间隔。  | 
|  `TIMESTAMP_SUB`  |  TIMESTAMP\$1SUB（标识符、间隔持续时间、列）  |  从日期时间表达式中减去以给定时间单位为单位的时间间隔。  | 
|  `CAST`  |  CAST（表达式为时间戳格式模式）  |  使用指定的格式模式将字符串表达式转换为时间戳。常见模式包括`'yyyy-MM-dd HH:mm:ss'`标准日期时间格式。例如，`SELECT CAST('2023-12-25 14:30:00' AS TIMESTAMP) AS converted_timestamp`  | 

**Example 使用列出的函数进行的 SQL 查询：**  

```
SELECT r.asset_id, r.int_value,
  date_add(DAY, 7, r.event_timestamp) AS date_in_future,
  date_sub(YEAR, 2, r.event_timestamp) AS date_in_past,
  timestamp_add(DAY, 2, r.event_timestamp) AS timestamp_in_future,
  timestamp_sub(DAY, 2, r.event_timestamp) AS timestamp_in_past,
  now() AS time_now
FROM raw_time_series AS r
```

# 类型转换函数
<a name="sql-functions-type-conv"></a>

 类型转换函数用于将值的数据类型从一种更改为另一种值。它们对于确保数据兼容性和执行需要特定格式数据的操作至关重要。


|  **函数**  |  **签名**  |  **描述**  | 
| --- | --- | --- | 
|  `TO_DATE`  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/iot-sitewise/latest/userguide/sql-functions-type-conv.html)  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/iot-sitewise/latest/userguide/sql-functions-type-conv.html)  | 
|  `TO_TIMESTAMP`  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/iot-sitewise/latest/userguide/sql-functions-type-conv.html)  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/iot-sitewise/latest/userguide/sql-functions-type-conv.html)  | 
|  `TO_TIME`  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/iot-sitewise/latest/userguide/sql-functions-type-conv.html)  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/iot-sitewise/latest/userguide/sql-functions-type-conv.html)  | 
|  `CAST`  |  演员阵容（<expression>饰演<data type>）  |  将计算结果为单个值的实体或表达式从一种类型转换为另一种类型。 支持的数据类型包括： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/iot-sitewise/latest/userguide/sql-functions-type-conv.html)  | 

**Example 使用列出的函数进行的 SQL 查询：**  

```
SELECT TO_TIMESTAMP (100) AS timestamp_value,
  TO_DATE(r.event_timestamp) AS date_value,
  TO_TIME(r.event_timestamp) AS time_value
FROM raw_time_series AS r
```