发布于 2016-08-17 16:46:02
如果您希望在上一次_之后的所有内容,那么使用:
select right(db_name(), charindex('_', reverse(db_name()) + '_') - 1)如果您之前想要所有的东西,那么使用left()
select left(db_name(), len(db_name()) - charindex('_', reverse(db_name()) + '_'))发布于 2018-01-18 08:24:02
编写了2个函数,1用于返回所选字符的LastIndexOf。
CREATE FUNCTION dbo.LastIndexOf(@source nvarchar(80), @pattern char)
RETURNS int
BEGIN
RETURN (LEN(@source)) - CHARINDEX(@pattern, REVERSE(@source))
END;
GO并在此LastIndexOf之前返回一个字符串。也许它会对某人有用。
CREATE FUNCTION dbo.StringBeforeLastIndex(@source nvarchar(80), @pattern char)
RETURNS nvarchar(80)
BEGIN
DECLARE @lastIndex int
SET @lastIndex = (LEN(@source)) - CHARINDEX(@pattern, REVERSE(@source))
RETURN SUBSTRING(@source, 0, @lastindex + 1)
-- +1 because index starts at 0, but length at 1, so to get up to 11th index, we need LENGTH 11+1=12
END;
GO发布于 2016-08-17 16:47:28
不,server没有LastIndexOf。
这是可用的字符串函数
但是您可以始终创建自己的函数。
CREATE FUNCTION dbo.LastIndexOf(@source text, @pattern char)
RETURNS
AS
BEGIN
DECLARE @ret text;
SELECT into @ret
REVERSE(SUBSTRING(REVERSE(@source), 1,
CHARINDEX(@pattern, REVERSE(@source), 1) - 1))
RETURN @ret;
END;
GO https://stackoverflow.com/questions/39002025
复制相似问题