Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Extend CHARINDEX with occurance matching


Extend CHARINDEX with occurance matching

Author
Message
Partha Pal
Partha Pal
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 52
Comments posted to this topic are about the item Extend CHARINDEX with occurance matching
James Goodwin
James Goodwin
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1399 Visits: 1107
This code seems to be overly complicated for the task presented. Why are you saving off the Chopped part of the input? If I needed this functionality I would keep track of the current position and use something like:


CREATE FUNCTION [dbo].[ufnGetCharIndexWithOccurance](@ToSearch VARCHAR(max), @InSearch VARCHAR(max), @Occurance INT=1)
RETURNS int
AS
--SELECT dbo.ufnGetCharIndexWithOccurance('AB', 'ABDABRCTAB', 5)
BEGIN
DECLARE @LoopCounter integer
DECLARE @CurrentLocation integer
DECLARE @result integer

SET @LoopCounter = 1
SET @result = 0
While @LoopCounter <= @Occurance BEGIN
Set @result = CHARINDEX(@ToSearch, @InSearch, @result+1)
IF @result = 0 --Occurance Not found
Set @LoopCounter = @Occurance + 1
ELSE
Set @LoopCounter = @LoopCounter + 1
END
RETURN @result
END



Note: I don't validate @Occurance. If @Occurance is not a valid input the While Condition will fail and the function will return 0.
--
JimFive
nigel.
nigel.
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1649 Visits: 2834
Way too complicated!
No need for any while loops just a good old Tally (Numbers) table:
DECLARE @search VARCHAR(20)
DECLARE @find VARCHAR(2)

SELECT @search = '-ABCDABFGHABXYZ.', @find = 'AB'

SELECT
occurence = ROW_NUMBER() OVER (ORDER BY n),
position = n ,
SUBSTRING(@search,n,LEN(@find))
FROM
dbo.Tally
WHERE
N < LEN(@search)
AND
SUBSTRING(@search,n,LEN(@find)) = @find



See the link in my sig for tally table info

--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

James Goodwin
James Goodwin
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1399 Visits: 1107
Ok, I thought about this a bit more and I don't like the loop. So I came up with the following that requires a Tally Table (A table of integers)


CREATE FUNCTION udfCharIndexPositionTally(@ToSearch varchar(max), @InSearch VARCHAR(max), @Occurance INT)
-- SELECT udfCharIndexPositionTally('AB','ABDABRCTAB',2) AS
RETURNS INT
DECLARE @return integer
SELECT @return = position
FROM (SELECT Row_Number() OVER(ORDER BY n) as RowNum
, n as position, SUBSTRING(@InSearch, n,Len(@ToSearch)) as compare
FROM TALLY
WHERE n>0 AND n <=Len(@InSearch)
AND SubString(@InSearch, n, Len(@ToSearch)) = @ToSearch) t
WHERE RowNum = @Occurance
RETURN @return


--
JimFive
nigel.
nigel.
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1649 Visits: 2834
James Goodwin (4/13/2011)
Ok, I thought about this a bit more and I don't like the loop. So I came up with the following that requires a Tally Table (A table of integers)


Ditto!

--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

Mitesh Oswal
Mitesh Oswal
SSC Eights!
SSC Eights! (870 reputation)SSC Eights! (870 reputation)SSC Eights! (870 reputation)SSC Eights! (870 reputation)SSC Eights! (870 reputation)SSC Eights! (870 reputation)SSC Eights! (870 reputation)SSC Eights! (870 reputation)

Group: General Forum Members
Points: 870 Visits: 653


DECLARE @Numbertable table
(
ID INT PRIMARY KEY

)


INSERT INTO @Numbertable
select TOP 1000 ROW_NUMBER() OVER(order by si.object_id)
from sys.objects si,sys.objects s

DECLARE @separatolog TABLE
(
SeparatorLogID INT,
ID INT IDENTITY(1,1) PRIMARY KEY
)
DECLARE @Str varchar(100) = 'ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE'

DECLARE @Separator VARCHAR(10) = ''

DECLARE @ValueAdd VARCHAR(10) = 'DEA'



INSERT INTO @separatolog
select distinct CHARINDEX(@ValueAdd,@str,ID)
from @Numbertable
where ID <= LEN(@str) and CHARINDEX(@ValueAdd,@str,ID) > 0
ORDER BY 1

select ID AS 'OCCURENCE',SeparatorLogID AS 'Exact Location' from @separatolog





Regards,
Mitesh OSwal
+918698619998
suryakant_ker
suryakant_ker
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 241
To get the number of times a particular string occurs in another string

DECLARE @STR VARCHAR(100) = 'ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE'
DECLARE @SEARCH VARCHAR(10) = 'A'

-- TO GET THE NUMBER OF OCCURRENCE COUNT
SELECT (LEN(@STR) - LEN(REPLACE(@STR,@SEARCH,'')))/LEN(@SEARCH) OCCURRENCECOUNT
Iwas Bornready
Iwas Bornready
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12146 Visits: 885
Thanks for the script.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search