LTRIM (Transact-SQL)
适用于:SQL ServerAzure SQL 数据库Azure SQL 托管实例Azure Synapse AnalyticsAnalytics Platform System (PDW)Microsoft Fabric 中的 SQL 分析终结点Microsoft Fabric 中的仓库
截断所有前导空格后返回一个字符串。
删除字符串开头的空格字符 char(32)
或其他指定字符。
语法
SQL Server 2022 (16.x) 之前的 SQL Server 语法:
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,text、ntext 和 image 除外。 否则,请使用 CAST 显式转换 character_expression。
characters
任何非 LOB 字符类型(nvarchar、varchar、nchar 或 char)的文本、变量或函数调用,其中包含应删除的字符。 不允许使用 nvarchar(max) 和 varchar(max) 类型。
返回类型
返回一个字符串自变量类型的字符表达式,其中已从 character_expression 开头删除空格字符 char(32)
或其他指定字符。 如果输入字符串是 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.
请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈