SQL 服务器中的 TrimEnd 等价物
TrimEnd Equivalent in SQL Server
我有列(数字),其值如下:
1,2,3
1,2,3,
1,2,3,,,
1,2,3,,,,,,
我想 Trim 字符串末尾的所有逗号,所以结果将是
1,2,3
1,2,3
1,2,3
1,2,3
我试过下面的查询,但这样我们只能删除最后一个逗号
DECLARE @String as VARCHAR(50)
SET @String='1,2,3,4,,,,,,,,,,,,,,,,'
SELECT CASE WHEN right(rtrim(@String),1) = ',' then substring(rtrim(@String),1,len(rtrim(@String))-1)
ELSE @String
END AS TruncString
如何删除字符串末尾的所有逗号?
因为多次出现,您不能使用简单的内置函数表达式来完成,但简单的用户定义函数可以完成这项工作。
create function dbo.MyTrim(@text varchar(max)) returns varchar(max)
as
-- function to remove all commas from the right end of the input.
begin
while (right(@text, 1) = ','
begin
set @text = left(@text, len(@text) - 1)
end
return @text
end
go
您可以搜索第一次出现的 ',,'
并获取之前的所有内容:
select (case when numbers like '%,,'
then left(numbers, charindex(',,', numbers) - 1)
when numbers like '%,'
then left(numbers, len(numbers) - 1)
else numbers
end)
注意:您似乎是在以逗号分隔的字符串中存储事物列表。通常最好使用联结 table.
来存储它们
编辑:
或者,另一种没有 case
的表述方式:
select left(numbers + ',,', charindex(',,', numbers + ',,') - 1)
您可以使用:
LEFT(Numbers, LEN(Numbers) - (PATINDEX('%[^,]%', REVERSE(Numbers)) - 1))
这样做的前提是你先用REVERSE
反转字符串:
REVERSE(Numbers) --> ,,,,,,3,2,1
然后使用 PATINDEX
和模式匹配 [^,]
:
找到第一个不是逗号的字符的位置
PATINDEX('%[^,]%', REVERSE(Numbers)) --> ,,,,,,3,2,1 = 7
然后就可以使用LEN
, to get the inverse position, i.e. if the position of the first character that is not a comma is 7 in the reversed string, and the length of the string is 10, then you need the first 4 characters of the string. You then use SUBSTRING
的字符串长度来提取相关部分
一个完整的例子是
SELECT Numbers,
Reversed = REVERSE(Numbers),
Position = PATINDEX('%[^,]%', REVERSE(Numbers)),
TrimEnd = LEFT(Numbers, LEN(Numbers) - (PATINDEX('%[^,]%', REVERSE(Numbers)) - 1))
FROM (VALUES
('1,2,3'),
('1,2,3,'),
('1,2,3,,,'),
('1,2,3,,,,,,'),
('1,2,3,,,5,,,'),
(',,1,2,3,,,5,,')
) t (Numbers);
编辑
为了响应编辑,在语法上有一些错误,下面有函数 trim 开始,trim 逗号两边:
SELECT Numbers,
Reversed = REVERSE(Numbers),
Position = PATINDEX('%[^,]%', REVERSE(Numbers)),
TrimEnd = LEFT(Numbers, LEN(Numbers) - (PATINDEX('%[^,]%', REVERSE(Numbers)) - 1)),
TrimStart = SUBSTRING(Numbers, PATINDEX('%[^,]%', Numbers), LEN(Numbers)),
TrimBothSide = SUBSTRING(Numbers,
PATINDEX('%[^,]%', Numbers),
LEN(Numbers) -
(PATINDEX('%[^,]%', REVERSE(Numbers)) - 1) -
(PATINDEX('%[^,]%', Numbers) - 1)
)
FROM (VALUES
('1,2,3'),
('1,2,3,'),
('1,2,3,,,'),
('1,2,3,,,,,,'),
('1,2,3,,,5,,,'),
(',,1,2,3,,,5,,')
) t (Numbers);
运行 下面查询得到预期结果
declare @sql varchar(500)
set @sql ='1,2,3,,,,,,'
select left(@sql,case charindex(',,',@sql,0)
when 0 then len(@sql)-1
else charindex(',,',@sql,0)-1
end)
Create FUNCTION TrimStartEndAll
(
@string varchar(max),
@trimValue varchar(5),
@removeall int=0
)
RETURNS varchar(max)
AS
BEGIN
if @removeall=1
while CHARINDEX(@trimValue,@string) >0 and @removeall=1
begin
set @string = REPLACE(@string,@trimValue,'')
end
if @removeall = 0
begin
while CHARINDEX(@trimValue,@string) =1
begin
set @string = SUBSTRING(@string,len(@trimValue)+1, len(@string))
end
while substring(@string,len(@string)-len(@trimValue)+1, len(@trimValue)) = @trimValue
begin
set @string =substring(@string,0, (len(@string)-len(@trimValue)+1))
end
end
return @string
END
GO
输出
select dbo.TrimStartEndAll( ',,1,2,3,,,5,,,,,,,,,',',,',1) => 1,2,3,5,
select dbo.TrimStartEndAll( ',,1,2,3,,,5,,,,,,,,,',',,',0) => 1,2,3,,,5,
SQL Server 2017实现了TRIM功能的增强版。
您可以使用 TRIM(',' FROM '1,2,3,') 获取字符串 '1,2,3'
我有列(数字),其值如下:
1,2,3
1,2,3,
1,2,3,,,
1,2,3,,,,,,
我想 Trim 字符串末尾的所有逗号,所以结果将是
1,2,3
1,2,3
1,2,3
1,2,3
我试过下面的查询,但这样我们只能删除最后一个逗号
DECLARE @String as VARCHAR(50)
SET @String='1,2,3,4,,,,,,,,,,,,,,,,'
SELECT CASE WHEN right(rtrim(@String),1) = ',' then substring(rtrim(@String),1,len(rtrim(@String))-1)
ELSE @String
END AS TruncString
如何删除字符串末尾的所有逗号?
因为多次出现,您不能使用简单的内置函数表达式来完成,但简单的用户定义函数可以完成这项工作。
create function dbo.MyTrim(@text varchar(max)) returns varchar(max)
as
-- function to remove all commas from the right end of the input.
begin
while (right(@text, 1) = ','
begin
set @text = left(@text, len(@text) - 1)
end
return @text
end
go
您可以搜索第一次出现的 ',,'
并获取之前的所有内容:
select (case when numbers like '%,,'
then left(numbers, charindex(',,', numbers) - 1)
when numbers like '%,'
then left(numbers, len(numbers) - 1)
else numbers
end)
注意:您似乎是在以逗号分隔的字符串中存储事物列表。通常最好使用联结 table.
来存储它们编辑:
或者,另一种没有 case
的表述方式:
select left(numbers + ',,', charindex(',,', numbers + ',,') - 1)
您可以使用:
LEFT(Numbers, LEN(Numbers) - (PATINDEX('%[^,]%', REVERSE(Numbers)) - 1))
这样做的前提是你先用REVERSE
反转字符串:
REVERSE(Numbers) --> ,,,,,,3,2,1
然后使用 PATINDEX
和模式匹配 [^,]
:
PATINDEX('%[^,]%', REVERSE(Numbers)) --> ,,,,,,3,2,1 = 7
然后就可以使用LEN
, to get the inverse position, i.e. if the position of the first character that is not a comma is 7 in the reversed string, and the length of the string is 10, then you need the first 4 characters of the string. You then use SUBSTRING
的字符串长度来提取相关部分
一个完整的例子是
SELECT Numbers,
Reversed = REVERSE(Numbers),
Position = PATINDEX('%[^,]%', REVERSE(Numbers)),
TrimEnd = LEFT(Numbers, LEN(Numbers) - (PATINDEX('%[^,]%', REVERSE(Numbers)) - 1))
FROM (VALUES
('1,2,3'),
('1,2,3,'),
('1,2,3,,,'),
('1,2,3,,,,,,'),
('1,2,3,,,5,,,'),
(',,1,2,3,,,5,,')
) t (Numbers);
编辑
为了响应编辑,在语法上有一些错误,下面有函数 trim 开始,trim 逗号两边:
SELECT Numbers,
Reversed = REVERSE(Numbers),
Position = PATINDEX('%[^,]%', REVERSE(Numbers)),
TrimEnd = LEFT(Numbers, LEN(Numbers) - (PATINDEX('%[^,]%', REVERSE(Numbers)) - 1)),
TrimStart = SUBSTRING(Numbers, PATINDEX('%[^,]%', Numbers), LEN(Numbers)),
TrimBothSide = SUBSTRING(Numbers,
PATINDEX('%[^,]%', Numbers),
LEN(Numbers) -
(PATINDEX('%[^,]%', REVERSE(Numbers)) - 1) -
(PATINDEX('%[^,]%', Numbers) - 1)
)
FROM (VALUES
('1,2,3'),
('1,2,3,'),
('1,2,3,,,'),
('1,2,3,,,,,,'),
('1,2,3,,,5,,,'),
(',,1,2,3,,,5,,')
) t (Numbers);
运行 下面查询得到预期结果
declare @sql varchar(500)
set @sql ='1,2,3,,,,,,'
select left(@sql,case charindex(',,',@sql,0)
when 0 then len(@sql)-1
else charindex(',,',@sql,0)-1
end)
Create FUNCTION TrimStartEndAll
(
@string varchar(max),
@trimValue varchar(5),
@removeall int=0
)
RETURNS varchar(max)
AS
BEGIN
if @removeall=1
while CHARINDEX(@trimValue,@string) >0 and @removeall=1
begin
set @string = REPLACE(@string,@trimValue,'')
end
if @removeall = 0
begin
while CHARINDEX(@trimValue,@string) =1
begin
set @string = SUBSTRING(@string,len(@trimValue)+1, len(@string))
end
while substring(@string,len(@string)-len(@trimValue)+1, len(@trimValue)) = @trimValue
begin
set @string =substring(@string,0, (len(@string)-len(@trimValue)+1))
end
end
return @string
END
GO
输出
select dbo.TrimStartEndAll( ',,1,2,3,,,5,,,,,,,,,',',,',1) => 1,2,3,5,
select dbo.TrimStartEndAll( ',,1,2,3,,,5,,,,,,,,,',',,',0) => 1,2,3,,,5,
SQL Server 2017实现了TRIM功能的增强版。 您可以使用 TRIM(',' FROM '1,2,3,') 获取字符串 '1,2,3'