Solved

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

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

778 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