Solved

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

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

749 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