SQL Server Equivalent to ORACLE INSTR
I wanted to know if in SQL Server there is an equivalent to the Oracle INSTR
function?
CHARINDEX
and PATINDEX
, but with the Oracle version I can also specify the Nth appearance of the character(s) I am looking for.
Oracle
INSTR
:instr( string1, string2 [, start_position [, **nth_appearance** ] ] )
The CHARINDEX
开发者_运维知识库almost gets me there, but I wanted to have it start at the nth_appearance
of the character in the string.
You were spot on that nth_appearance
does not exist in SQL Server.
Shamelessly copying a function (Equivalent of Oracle's INSTR with 4 parameters in SQL Server) created for your problem (please note that @Occurs is not used the same way as in Oracle - you can't specify "3rd appearance", but "occurs 3 times"):
CREATE FUNCTION udf_Instr
(@str1 varchar(8000), @str2 varchar(1000), @start int, @Occurs int)
RETURNS int
AS
BEGIN
DECLARE @Found int, @LastPosition int
SET @Found = 0
SET @LastPosition = @start - 1
WHILE (@Found < @Occurs)
BEGIN
IF (CHARINDEX(@str1, @str2, @LastPosition + 1) = 0)
BREAK
ELSE
BEGIN
SET @LastPosition = CHARINDEX(@str1, @str2, @LastPosition + 1)
SET @Found = @Found + 1
END
END
RETURN @LastPosition
END
GO
SELECT dbo.udf_Instr('x','axbxcxdx',1,4)
GO
DROP FUNCTION udf_Instr
GO
Here is a version of Oracle's INSTR function which also works with a negative position for a reverse lookup as per Oracle's Doc here :- https://docs.oracle.com/cd/B28359_01/olap.111/b28126/dml_functions_1103.htm#OLADM564
CREATE FUNCTION dbo.INSTR(@str NVARCHAR(MAX), @substr NVARCHAR(MAX), @position INT = 1, @occurance INT = 1)
RETURNS INT
AS
BEGIN
DECLARE @loc INT = @position;
IF @loc < 0
BEGIN
SET @str = REVERSE(@str);
SET @substr = REVERSE(@substr);
SET @loc = @loc * -1;
END
IF @loc > 0
BEGIN
SET @loc = @loc - 1;
END
WHILE (@occurance > 0 AND CHARINDEX(@substr, @str, @loc + 1) > 0)
BEGIN
SET @loc = CHARINDEX(@substr, @str, @loc + 1);
SET @occurance = @occurance - 1;
END
IF @occurance > 0
BEGIN
SET @loc = 0;
END
IF @position < 0
BEGIN
SET @loc = LEN(@str) - @loc;
END
RETURN @loc
END
Change @str1 varchar(8000), @str2 varchar(1000)
to @str1 varchar(1000), @str2 varchar(8000)
or
change CHARINDEX(@str1, @str2, @LastPosition + 1)
to CHARINDEX(@str2, @str1, @LastPosition + 1)
You can use the following UDF (inline function rather than scalar)
CREATE FUNCTION dbo.INSTR
(
@str VARCHAR(8000),
@Substr VARCHAR(1000),
@start INT ,
@Occurance INT
)
RETURNS TABLE
AS
RETURN
WITH Tally (n) AS
(
SELECT TOP (LEN(@str)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM (VALUES (0),(0),(0),(0),(0),(0),(0),(0)) a(n)
CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b(n)
CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) c(n)
CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) d(n)
)
, Find_N_STR as
(
SELECT
CASE WHEN DENSE_RANK() OVER(PARTITION BY @Substr ORDER BY (CHARINDEX(@Substr ,@STR ,N))) = @Occurance
THEN MAX(N-@start +1) OVER (PARTITION BY CHARINDEX(@Substr ,@STR ,N) )
ELSE 0
END [Loc]
FROM Tally
WHERE CHARINDEX(@Substr ,@STR ,N) > 0
)
SELECT Loc= MAX(Loc)
FROM Find_N_STR
WHERE Loc > 0
How to use:
declare @T table
(
Name_Level_Class_Section varchar(25)
)
insert into @T values
('Jacky_1_B2_23'),
('Johnhy_1_B2_24'),
('Peter_2_A5_3')
select t.Name_Level_Class_Section , l.Loc
from @t t
cross apply dbo.INSTR (t.Name_Level_Class_Section, '_',1,2) l
Try this !!
CREATE FUNCTION dbo.INSTR (@str VARCHAR(8000), @substr VARCHAR(255), @start INT, @occurrence INT)
RETURNS INT
AS
BEGIN
DECLARE @found INT = @occurrence,
@pos INT = @start;
WHILE 1=1
BEGIN
-- Find the next occurrence
SET @pos = CHARINDEX(@substr, @str, @pos);
-- Nothing found
IF @pos IS NULL OR @pos = 0
RETURN @pos;
-- The required occurrence found
IF @found = 1
BREAK;
-- Prepare to find another one occurrence
SET @found = @found - 1;
SET @pos = @pos + 1;
END
RETURN @pos;
END
GO
Usage :
-- Find the second occurrence of letter 'o'
SELECT dbo.INSTR('Moscow', 'o', 1, 2);
-- Result: 5
精彩评论