

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 純量函數
<a name="sql-functions-scalar"></a>

 純量函數會取得一或多個輸入值，並傳回單一輸出值。它們廣泛用於 SQL （結構化查詢語言） 以進行資料處理和擷取，從而提高資料處理任務的效率。

**Topics**
+ [Null 資料函數](sql-functions-null.md)
+ [字串函數](sql-functions-string.md)
+ [數學函數](sql-functions-math.md)
+ [日期時間函數](sql-functions-date.md)
+ [類型轉換函數](sql-functions-type-conv.md)

# Null 資料函數
<a name="sql-functions-null"></a>

 Null 資料函數會處理或操作 NULL 值，這表示沒有值。函數可讓您將 NULLs 取代為其他值、檢查值是否為 NULL，或執行以特定方式處理 NULLs 的操作。


|  **函數**  |  **Signature**  |  **Description**  | 
| --- | --- | --- | 
|  `COALESCE`  |   COALESCE (expression1、 expression2、...、 expressionN)   |  如果所有表達式都評估為 null，則 COALESCE 會傳回 null。運算式必須為相同類型。  | 

**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>

 字串函數是用於操作和處理文字資料的內建工具。它們可在字串中啟用串連、擷取、格式化和搜尋等任務。這些函數對於清理、轉換和分析資料庫中以文字為基礎的資料至關重要。


**字串函數**  

|  **函數**  |  **Signature**  |  **Description**  | 
| --- | --- | --- | 
|  `LENGTH`  |   LENGTH （字串）   |  傳回字串的長度。  | 
|  `CONCAT`  |   CONCAT （字串、字串）   |  串連字串中的引數。  | 
|  `SUBSTR`  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-sitewise/latest/userguide/sql-functions-string.html)  |  傳回下列其中一項： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-sitewise/latest/userguide/sql-functions-string.html) 使用以 1 為基礎的索引做為啟動參數。  | 
|  `UPPER`  |   UPPER （字串）   |  將輸入字串中的字元轉換為大寫。  | 
|  `LOWER`  |   LOWER （字串）   |  將輸入字串中的字元轉換為小寫。  | 
|  `TRIM`  |   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_tw/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\$1REPLACE  |   `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 查詢中用來對數值資料執行計算的預先定義數學操作。它們提供操作和轉換資料的方法，而不需要從資料庫擷取資料並單獨處理資料。


**數學函數**  

|  **函數**  |  **Signature**  |  **Description**  | 
| --- | --- | --- | 
|  `POWER`  |  POWER (int\$1double、int\$1double)  |  傳回提升為第二個引數能力的第一個引數值。  | 
|  `ROUND`  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-sitewise/latest/userguide/sql-functions-math.html)  |  四捨五入至最接近的整數。  | 
|  `FLOOR`  |   FLOOR (int\$1double)   |  傳回不超過指定值的最大整數。  | 

所有 函數的範例：


|  **函數**  |  **範例**  | 
| --- | --- | 
|  POWER  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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


|  **函數**  |  **Signature**  |  **Description**  | 
| --- | --- | --- | 
|  `NOW`  |   現在 ( )   |  以毫秒精確度傳回目前的時間戳記。它提供在查詢中執行的確切時間。  | 
|  `DATE_ADD`  |  DATE\$1ADD （識別符、 interval\$1duration、資料欄）  |  傳回日期/時間和天數/小時的總和，或日期/時間和日期/時間間隔的總和。  | 
|  `DATE_SUB`  |  DATE\$1SUB （識別符、 interval\$1duration、資料欄）  |  傳回日期/時間和天數/小時之間的差異，或日期/時間和日期/時間間隔之間的差異。  | 
|  `TIMESTAMP_ADD`  |  TIMESTAMP\$1ADD （識別符、 interval\$1duration、資料欄）  |  將指定時間單位的時間間隔新增至日期時間表達式。  | 
|  `TIMESTAMP_SUB`  |  TIMESTAMP\$1SUB （識別符、 interval\$1duration、資料欄）  |  從日期時間表達式減去指定時間單位的時間間隔。  | 
|  `CAST`  |  CAST （表達式 AS TIMESTAMP FORMAT 模式）  |  使用指定的格式模式將字串表達式轉換為時間戳記。標準日期時間格式`'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>

 類型轉換函數用於將值的資料類型從一個變更為另一個。它們對於確保資料相容性和執行需要特定格式資料的操作至關重要。


|  **函數**  |  **Signature**  |  **Description**  | 
| --- | --- | --- | 
|  `TO_DATE`  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-sitewise/latest/userguide/sql-functions-type-conv.html)  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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_tw/iot-sitewise/latest/userguide/sql-functions-type-conv.html)  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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_tw/iot-sitewise/latest/userguide/sql-functions-type-conv.html)  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-sitewise/latest/userguide/sql-functions-type-conv.html)  | 
|  `CAST`  |  CAST (<expression> AS <data type>)  |  將評估為單一值的實體或表達式，從一種類型轉換為另一種類型。 支援的資料類型如下： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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
```