?
Solved

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

Posted on 2008-10-13
3
Medium Priority
?
3,662 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

762 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