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
 General SQL Server Forums
 New to SQL Server Programming
 Fetch Data
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

nirene
Yak Posting Veteran

India
92 Posts

Posted - 02/28/2013 :  00:56:35  Show Profile  Reply with Quote
Hi,

Data

Jaguar||61221031||4250||121725.13||9075129536
Land Rover||8984FFF498989||7878||890990||8977777
Range Rover||676767HQW7||8980||456567||999999999999999

Result expected

Jaguar,61221031
Land Rover,8984FFF498989
Range Rover,676767HQW7

I order to get the first column I used
SELECT LEFT(Fld, CHARINDEX('||', Fld) - 1) as First

How to get the second column?

Thanks in advance

Nirene

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 02/28/2013 :  04:00:00  Show Profile  Reply with Quote

declare @test table
(
Data varchar(100)
)
insert @test
values ('Jaguar||61221031||4250||121725.13||9075129536')
,('Land Rover||8984FFF498989||7878||890990||8977777')
,('Range Rover||676767HQW7||8980||456567||999999999999999')

SELECT m.n.value('./Node[1]','varchar(100)') AS [Column1],
m.n.value('./Node[2]','varchar(100)') AS [Column2]
FROM (SELECT CAST('<Root><Node>' + REPLACE(Data,'||','</Node><Node>') + '</Node></Root>' AS xml) AS XMLData FROM @test)t
CROSS APPLY XmlData.nodes('/Root') m(n) 


output
------------------------------
Column1	        Column2
------------------------------
Jaguar	        61221031
Land Rover	8984FFF498989
Range Rover	676767HQW7



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

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.03 seconds. Powered By: Snitz Forums 2000