Solved

T-SQL to find position of second occurence of a substring in a string

Posted on 2008-10-13
3
3,606 Views
Last Modified: 2012-05-05
Hi,
 I need a T-SQL script  to find position of second occurence of a substring in a string
0
Comment
Question by:david_32
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 22708958
you have to use CHARINDEX 2 times..
DECLARE @search_string VARCHAR(100)
DECLARE @needle VARCHAR(100)
SET @needle = 's'
SET @search_String = 'string in s1' 
SELECT @search_string, @needle, CHARINDEX(@needle, @search_string, 1+ charindex(@needle, @search_string) )

Open in new window

0
 
LVL 28

Expert Comment

by:imran_fast
ID: 22709000
you can use the charindex function
below is the example of how to find second location of str in stringstr
select charindex('str','stringstr',charindex('str','stringstr')+1)

Open in new window

0
 
LVL 1

Expert Comment

by:Kumawat_Shobha
ID: 22709366
try it like this
declare @str      varchar(60),
      @str1      varchar(10)

select      @str      =      'nehashobhasnehashobhatripthi',
      @str1      =      'shobha'

select charindex(@str1,@str,charindex(@str1,@str)+1)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now