LTRIM (Transact-SQL)

適用於:Microsoft Fabric 中 Microsoft Fabric倉儲中的 SQL ServerAzure SQL 資料庫 Azure SQL 受控執行個體 Azure SynapseAnalytics Analytics Platform System (PDW)SQL 分析端點

傳回截斷所有前置空格的字元字串。

從字串的開頭移除空白字元 char(32) 或其他指定的字元。

Transact-SQL 語法慣例

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,才能使用選擇性 characters 引數。

LTRIM ( character_expression , [ characters ] )

注意

若要檢視 SQL Server 2014 (12.x) 和舊版的 Transact-SQL 語法,請參閱 舊版檔

引數

character_expression

字元或二進位資料的運算式character_expression 可以是常數、變數或資料行。 character_expression 必須是可隱含地轉換為 varchar的資料類型,但是 textntextimage 除外。 否則,請使用 CAST 來明確轉換 character_expression

characters

應移除所含字元的任何非 LOB 字元類型 (NvarcharvarcharNcharchar) 的常值、變數或函式呼叫。 不允許 nvarchar(max)varchar(max) 類型。

傳回類型

以字串引數的類型傳回字元運算式,其中空白字元 char(32) 或其他指定的字元會從 character_expression 開頭移除。 如果輸入字串為 NULL,傳回 NULL

備註

若要啟用選擇性 characters 位置引數,請在執行查詢時所連線的資料庫上啟用資料庫相容性層級 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,才能使用選擇性 characters 引數。

下列範例會從 123abc. 字串開頭移除 123 字元。

SELECT LTRIM('123abc.' , '123.');

以下為結果集。

abc.

另請參閱