Solved

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

Posted on 2008-10-13
3
3,656 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 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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

696 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