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
 stuff in sql
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

peace
Constraint Violating Yak Guru

404 Posts

Posted - 06/24/2013 :  23:58:31  Show Profile  Reply with Quote
how can i only grab only the ID 65489464678974667?
<HELLO a="65489464678974667"/><AMT a="2546.00"/>

sometimes the ID leght might not be same, example:
<HELLO a="ABC999"/><AMT a="3243.00"/>
<HELLO a="3874KIT"/><AMT a="5654.00"/>

bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 06/25/2013 :  00:28:28  Show Profile  Reply with Quote
DECLARE @XML XML ='<HELLO a="ABC999"/><AMT a="3243.00"/><HELLO a="3874KIT"/><AMT a="5654.00"/><HELLO a="65489464678974667"/><AMT a="2546.00"/>'
SELECT M.n.value('(@a)[1]' , 'VARCHAR(50)')
FROM @XML.nodes('/HELLO') M(n)


--
Chandu
Go to Top of Page

peace
Constraint Violating Yak Guru

404 Posts

Posted - 06/25/2013 :  00:47:28  Show Profile  Reply with Quote
There are alot of data...
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17595 Posts

Posted - 06/25/2013 :  01:00:19  Show Profile  Reply with Quote
Cross Apply it

with
cte as
(
	select	xmlcol = convert(xml, thecol)
	from	yourtbl
)
select	M.n.value('(@a)[1]' , 'VARCHAR(50)')
from	cte t
	cross apply xmlcol.nodes('/HELLO') as M(n)



KH
Time is always against us


Edited by - khtan on 06/25/2013 02:34:29
Go to Top of Page

peace
Constraint Violating Yak Guru

404 Posts

Posted - 06/25/2013 :  02:33:52  Show Profile  Reply with Quote
i get this error.
what it meant ya?

XML parsing: line 1, character 73, illegal qualified name character
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17595 Posts

Posted - 06/25/2013 :  02:34:47  Show Profile  Reply with Quote
quote:
Originally posted by peace

i get this error.
what it meant ya?

XML parsing: line 1, character 73, illegal qualified name character



sorry, missed that with


KH
Time is always against us

Go to Top of Page

peace
Constraint Violating Yak Guru

404 Posts

Posted - 06/25/2013 :  02:43:01  Show Profile  Reply with Quote
Yup, i added in the 'WITH'

i get that error. is the converting issue?
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17595 Posts

Posted - 06/25/2013 :  02:55:44  Show Profile  Reply with Quote
your data probably contains characters like &.

What is source of these data ? is it XML ?


KH
Time is always against us

Go to Top of Page

peace
Constraint Violating Yak Guru

404 Posts

Posted - 06/25/2013 :  03:01:40  Show Profile  Reply with Quote
is nvarchar..
i tried convert it to xml but getting that error.
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 06/25/2013 :  03:05:47  Show Profile  Reply with Quote
Refer is link
http://stackoverflow.com/questions/9920792/how-to-parse-xml-with-special-character-in-sql-server

--
Chandu
Go to Top of Page

peace
Constraint Violating Yak Guru

404 Posts

Posted - 06/25/2013 :  04:05:26  Show Profile  Reply with Quote
how can i update to xml character as all data are different.
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17595 Posts

Posted - 06/25/2013 :  04:14:38  Show Profile  Reply with Quote
alternatively use charindex(), substring() to parse the string and extract the required data


KH
Time is always against us

Go to Top of Page

peace
Constraint Violating Yak Guru

404 Posts

Posted - 06/25/2013 :  04:53:01  Show Profile  Reply with Quote
Can I apply the logic like if found 'HELLO a=' then grab the ID output..
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 06/25/2013 :  05:05:06  Show Profile  Reply with Quote
can you try this?


with
cte as
(
	select	xmlcol = convert(xml, '<Root>' + LTRIM(RTRIM(thecol)) + '</Root>')
	from	yourtbl
)
select	M.n.value('(@a)[1]' , 'VARCHAR(50)')
from	cte t
	cross apply xmlcol.nodes('/Root/HELLO') as M(n)



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

peace
Constraint Violating Yak Guru

404 Posts

Posted - 06/25/2013 :  05:11:28  Show Profile  Reply with Quote
Im getting this error:

XML parsing: line 1, character 79, illegal qualified name character
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 06/25/2013 :  05:24:04  Show Profile  Reply with Quote
Ok then thats out of question

wat about this?


SELECT REPLACE(LEFT(theCol,CHARINDEX('"/>',thecol,CHARINDEX('<HELLO a="',thecol))-1),'<HELLO a="','') FROM Table


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

peace
Constraint Violating Yak Guru

404 Posts

Posted - 06/25/2013 :  05:39:42  Show Profile  Reply with Quote
im getting it now.
but can you explaint how it works?

some of the data appear like this:

<DESC v="gsgserg"/><AMT v="33.50"/><HELLO v="234231512351"/><Dcc::Applicable v="False"/>

where the HELLO v="... is in between.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 06/25/2013 :  05:51:57  Show Profile  Reply with Quote
quote:
Originally posted by peace

im getting it now.
but can you explaint how it works?

some of the data appear like this:

<DESC v="gsgserg"/><AMT v="33.50"/><HELLO v="234231512351"/><Dcc::Applicable v="False"/>

where the HELLO v="... is in between.



you should have specified this in your first post itself as we cant guess how all values are coming in your case

SELECT  LEFT(theCol,CHARINDEX('"/>',thecol)-1)
FROM 
(SELECT STUFF(theCol,1,CHARINDEX('<HELLO a="',theCol)+ 10,'') AS theCol FROM Table)t



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
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.11 seconds. Powered By: Snitz Forums 2000