Geoffrey Emery
Tech Goodness

Instr/InString translation for T-Sql

May 6, 2008 15:39 by gemery

I just ran over some code I remembered grabbing from the web forever ago i thought i would share  the wealth. It works like a charm!

USE [EmailGenerator]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
CREATE  function [dbo].[InString](
 
      @string varchar(200), 
      @searchfor varchar(50), 
      @position int
      ) returns int
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Returns the position of the character AFTER the nth instance 
of the string
 
-- TEST CASE #1
-- should return 18
select dbo.InString('123456 123456 123456 123456', '23', 3) 
                    as [Test Case #1]
 
-- TEST CASE #2
-- should return 25
select dbo.InString('test1/test2/test3/test4/test5/', '/', 4) 
                     as [Test Case #2]
 
-- TEST CASE #3
declare @teststring varchar(50)
set @teststring = 'test1/test2/test3/test4/test5/'
select substring(@teststring, dbo.Instring(@teststring,'/',3),5)
                     as [Test Case #3]
-- should return 'test4'
 
-- TEST CASE #4 (variable length delimited fields
declare @teststring2 varchar(50)
set @teststring2 = 'test123/test/testtestestest/testxyz/test/'
select substring( @teststring2, 
                  dbo.Instring(@teststring2, '/', 3),
                  (dbo.Instring(@teststring2, '/', 4) -1) 
                           - dbo.Instring(@teststring2, '/', 3)
                  ) as [Test Case #4]
-- should return 'testxyz'
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
 
as
begin
declare @lenstring int, @poscount int, @stringpos int
set @lenstring = datalength(@searchfor)
set @poscount = 1
set @stringpos = 1
while @poscount <= @position and @stringpos <= len(@string) 
      begin
            -- if we find the string segment we're looking for 
        if substring(@string, @stringpos, @lenstring)=@searchfor
                  begin
                -- is the instance of the string the one we are
                --  looking for?
                        if @poscount = @position 
                              begin
                        set @stringpos = @stringpos + @lenstring
                                    return @stringpos
                              end
                -- else look for the next instance of the string
                -- segment
                        else 
                              begin
                                    set @poscount = @poscount + 1
                              end
                  end
            set @stringpos = @stringpos + 1
      end
return null
end
 

 

Technorati Tags: ,,,

Tags: ,
Categories: T-SQL
Actions: E-mail | Permalink | Comments (20) | Comment RSSRSS comment feed

Related posts

Comments

June 30. 2008 07:26

MarQ

Thanks for sharing. I was looking for this so I woulnd have to do it.

MarQ

March 26. 2009 13:37

ClubbieTim

CREATE FUNCTION [dbo].[FN_IN_STRING](

@string VARCHAR(200),
@searchfor VARCHAR(50),
@position INT
) RETURNS INT
AS

--DECLARE @string VARCHAR(200), @searchfor VARCHAR(50), @position INT
--SELECT @string = 'PHX_D_TEXT', @searchfor = '_', @position = '0'

BEGIN
DECLARE @lenstring INT, @I INT, @X INT
SET @lenstring = LEN(@searchfor); SET @I = @position; SET @X = LEN(@string)

WHILE @I <= @X
BEGIN
IF (SUBSTRING(@string, @I, @lenstring)= @searchfor) RETURN @I
SET @I = @I + 1
END
RETURN NULL
END

ClubbieTim

March 26. 2009 13:37

ClubbieTim

This version actually works...Nice try though...

ClubbieTim

August 6. 2009 05:23

Wealthy affiliate

Thank you for having the time and dedication in putting up blogs such as this one! It is deeply appreciated! Keep up the good work!

Wealthy affiliate

August 21. 2009 23:08

juegos en el casino virtual

I found your site on delicious today and really liked it.. i bookmarked it and will be back to check it out some more later ..

juegos en el casino virtual

October 5. 2009 03:39

cheap aion online leveling

I digged this for more news from you.



Regards and respect
Arvin

cheap aion online leveling

October 5. 2009 23:55

warcraft power level

Hi nice site design


Regards

frio

warcraft power level

October 6. 2009 00:04

Sex Harassment Attorneys California

This is the best post on this topic i have ever read.


Regards

pop

Sex Harassment Attorneys California

October 12. 2009 02:47

watch scrubs online

This is the best post on this topic i have ever read.


Regards

Morgan

watch scrubs online

October 18. 2009 23:42

washington state dui attorneys

Hi nice POst

Regards

Gursh

washington state dui attorneys

October 29. 2009 07:14

Holiday Lighting Raleigh

Hi nice POst

Regards

Clemons


Holiday Lighting Raleigh

October 29. 2009 21:08

Jeff Paul Scam

Great information. Thanks very much.

Jeff Paul Scam

October 30. 2009 17:32

Wealthy Affiliate

Great information. Thanks very much.

Wealthy Affiliate

October 31. 2009 23:56

easy personal loans

I like what I see. keep it going

easy personal loans

November 1. 2009 20:03

denver dui attorney

This is a good tool for translating the instring for T sql. Thank you for having the time and dedication in putting up blogs such as this one! It is deeply appreciated! Keep up the good work!. These codes will be really helpful.

denver dui attorney

November 5. 2009 05:28

faxless payday loans

Yea nice Work !Laughing

faxless payday loans

November 8. 2009 10:27

Wealthy Affiliate Reviews

Good thing you remembered this stuff coz I have been looking for this info everywhere....Thanks

Wealthy Affiliate Reviews

November 9. 2009 12:39

pay day loans

I just hope to have understood this the way it was meant

pay day loans

November 10. 2009 04:48

debt consolidation loan

Nice information, many thanks to the author. It is incomprehensible to me now, but in general, the usefulness and significance is overwhelming. Thanks again and good luck!

debt consolidation loan

November 10. 2009 20:51

pass a drug test

Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information.

pass a drug test

Comments are closed