SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 substing for first, middle and end text
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

srkhan30
Starting Member

United Kingdom
3 Posts

Posted - 07/27/2012 :  04:41:01  Show Profile  Reply with Quote
I have a row which has column Names -
data: 'Mike Smith;John Ward;David Law'

I need to saperate out to three rows each row should have 1 name.

--1st Query
declare @String varchar(500)
set @String = 'Mike Smith;Johnathan Ward;David Law'
select substring(@String, 0,charindex(';',@String)) AS Names
union all

--2nd Query
select substring(@String, patindex('%;%',@String)+1, patindex('%;%',reverse(@String))) AS Names
union all

--3rd Query
select right(@String,patindex('%;%',reverse(@String))-1) AS Names

The column Names should return:
Mike Smith
Johnathan Ward
David Law

The first row and third row are fine, I have having problem in second which is middle row, middle is returning some part of it not complete name,could someone please help me. Many Thanks.

khtan
In (Som, Ni, Yak)

Singapore
16745 Posts

Posted - 07/27/2012 :  04:51:24  Show Profile  Reply with Quote
use fnParseList
from http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033


KH
Time is always against us

Go to Top of Page

srkhan30
Starting Member

United Kingdom
3 Posts

Posted - 07/27/2012 :  05:18:01  Show Profile  Reply with Quote
I want in substing functions or left and right.
fnParseList will be bit over coding.
quote:
Originally posted by khtan

use fnParseList
from http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033


KH
Time is always against us





Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16745 Posts

Posted - 07/27/2012 :  05:20:08  Show Profile  Reply with Quote
you can just use it as it is. It will give you the result that you required nicely.


KH
Time is always against us

Go to Top of Page

srkhan30
Starting Member

United Kingdom
3 Posts

Posted - 07/27/2012 :  05:29:54  Show Profile  Reply with Quote
give me a example of it, i cant use this function.

quote:
Originally posted by khtan

you can just use it as it is. It will give you the result that you required nicely.


KH
Time is always against us



Go to Top of Page

Rajana
Starting Member

India
2 Posts

Posted - 07/27/2012 :  05:40:20  Show Profile  Reply with Quote
yes
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16745 Posts

Posted - 07/27/2012 :  06:04:02  Show Profile  Reply with Quote
quote:
Originally posted by srkhan30

give me a example of it, i cant use this function.

quote:
Originally posted by khtan

you can just use it as it is. It will give you the result that you required nicely.


KH
Time is always against us







why ?


KH
Time is always against us

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000