--oracle 
select rtrim('where aa=0 and b=1 and','and')
--要求获得
where aa=0 and b=1

在sql中没有类似于oracle的rtrim(,)的方法

故用以下方法

--SQL
declare @s nvarchar(50)
declare @dd nvarchar(50)
declare @sp_name nvarchar(50)

set @sp_name='where aa=0 and b=1 and bb=3 and'
select @sp_name
set @dd=reverse(@sp_name)
set @s=substring(@dd,charindex('a',@dd,0)+1,charindex('w',@dd,0))
select reverse(@s)

其中

reverse() 为反转函数

substring(str,start,length)截取指定长度的字符串

 

肖工写的函数

CREATE FUNCTION dbo.Rtrimstring
(@string NVARCHAR(4000),@trimStr NVARCHAR(50))
RETURNS NVARCHAR(4000)
AS
BEGIN
set @string =rtrim(isnull(@string ,''))
WHILE (Len (@string) > 0)
BEGIN
IF RIGHT(@string,Len(@trimStr)) = @trimStr
BEGIN
--与去掉前导字符串函数正好相反,截取的时候是从左侧截取,从而忽略尾部的匹配字符串
SET @string = LEFT(@string,Len(@string) - Len(@trimStr))
END
ELSE
BREAK
END
RETURN @string
END



DECLARE @string NVARCHAR(4000),@trimStr NVARCHAR(50);
SET @string='where aa=0 and b=1 and';
SET @trimStr='and';
select @string=dbo.Rtrimstring(@string,@trimStr);
select @string;



资料来源:http://www.51itr.com/index.php?doc-view-82.html

大家还有更好的办法么

相关文章:

  • 2021-07-29
  • 2022-01-01
  • 2021-11-26
  • 2021-07-12
  • 2022-12-23
  • 2021-04-19
  • 2021-08-13
  • 2021-12-13
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-19
  • 2021-08-18
相关资源
相似解决方案