

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

# 字串函數
<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
```