--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
大家还有更好的办法么