適用於:SQL Server
Azure SQL 資料庫
Azure SQL 受控執行個體
Azure Synapse Analytics
分析平台系統(PDW)
Microsoft Fabric 中的 SQL 分析端點
Microsoft Fabric 中的倉儲
Microsoft Fabric 中的 SQL 資料庫
傳回截斷所有前置空格的字元字串。
從字串的開頭移除空白字元 char(32) 或其他指定的字元。
Syntax
SQL Server 2022 之前的 SQL Server 語法 (16.x):
LTRIM ( character_expression )
SQL Server 2022 (16.x) 和更新版本的語法、Azure SQL 受控執行個體、Azure SQL 資料庫、Azure Synapse Analytics 和 Microsoft Fabric:
重要
您需要將資料庫相容性層級設定為 160,才能使用選擇性 字元 自變數。
LTRIM ( character_expression , [ characters ] )
引數
character_expression
字元或二進位資料的運算式。 character_expression 可以是常數、變數或資料行。 character_expression 必須是可隱含地轉換為 varchar的資料類型,但是 textntext 和 image 除外。 否則,請使用 CAST 來明確轉換 character_expression。
characters
應移除所含字元的任何非 LOB 字元類型 (Nvarchar、varchar、Nchar 或 char) 的常值、變數或函式呼叫。 不允許 nvarchar(max) 和 varchar(max) 類型。
傳回類型
以字串引數的類型傳回字元運算式,其中空白字元 char(32) 或其他指定的字元會從 character_expression 開頭移除。 如果輸入字串為 NULL,傳回 NULL。
備註
若要啟用選擇性 字元 位置自變數,請在執行查詢時連接到的資料庫上啟用資料庫相容性層級 160 。
範例
A. 移除前置空格
下列範例會利用 LTRIM 來移除字元運算式中的開頭空白。
SELECT LTRIM(' Five spaces are at the beginning of this string.');
結果集如下所示。
---------------------------------------------------------------
Five spaces are at the beginning of this string.
B:使用變數移除前置空格
下列範例會利用 LTRIM 來移除字元變數中的開頭空白。
DECLARE @string_to_trim VARCHAR(60);
SET @string_to_trim = ' Five spaces are at the beginning of this string.';
SELECT
@string_to_trim AS 'Original string',
LTRIM(@string_to_trim) AS 'Without spaces';
GO
結果集如下所示。
Original string Without spaces
----------------------------------------------------- ---------------------------------------------
Five spaces are at the beginning of this string. Five spaces are at the beginning of this string.
C. 從字串開頭移除指定的字元
重要
您需要將資料庫相容性層級設定為 , 160 才能使用選擇性 字元 自變數。
下列範例會從 123 字串開頭移除 123abc. 字元。
SELECT LTRIM('123abc.' , '123.');
結果集如下所示。
abc.