querying for text in an XML field
  Home FAQ Contact Sign in
microsoft.public.sqlserver.xml only
 
Advanced search
POPULAR GROUPS

more...

microsoft.public.sqlserver.xml Profile…
 Up
querying for text in an XML field         


Author: Steven
Date: Aug 18, 2008 12:36

Using SQL 2005

I've not much experience with XML and SQL 2005. I have a field of XML
datatype. How do I look for a specific string within the XML field?

Thanks!
4 Comments
Re: querying for text in an XML field         


Author: Steven
Date: Aug 18, 2008 13:00

Here is fragment of th data in my xml field. I am looking to get the value
for mediatype.

http://xxx.org/schemas/03/2005/xxxx Version="5.2.9">



Text

.

.

.

.

"Steven" hotmail.com> wrote in message
news:epdHDnWAJHA.1184@TK2MSFTNGP04.phx.gbl...
> Using SQL 2005
>
> I've not much experience with XML and SQL 2005. I have a field of XML
> datatype. How do I look for a specific string within the XML field?
>
> Thanks!
>
no comments
Re: querying for text in an XML field         


Author: Jacob Sebastian
Date: Aug 18, 2008 19:41

Here is an example:

DECLARE @x XML
SELECT @x = '
http://xxx.org/schemas/03/2005/xxxx" Version="5.2.9">

Text

'

;WITH XMLNAMESPACES(
DEFAULT 'http://xxx.org/schemas/03/2005/xxxx'
)
SELECT @x.value('(Publication/Identification/MediaType)[1]','VARCHAR(20)')
AS MediaType
/*
MediaType
--------------------
Text
*/
Show full article (1.31Kb)
no comments
Re: querying for text in an XML field         


Author: Bob
Date: Aug 18, 2008 20:17

To retrieve values for an XML column, you use the value method of the XML
data-type against the column, eg

DECLARE @xml XML

--SET @xml = 'http://xxx.org/schemas/03/2005/xxxx"
Version="5.2.9">
SET @xml = '
balls
test

'

DECLARE @table TABLE( id INT IDENTITY PRIMARY KEY, your_xml XML )

INSERT INTO @table VALUES ( @xml )

SELECT
t.your_xml.value( '(Publication/Identification/MediaType)[1]','VARCHAR(20)' )
FROM @table t

I had trouble getting this to work with your namespace declaration, which is
why I've commented it out.
HTH
wBob
Show full article (1.28Kb)
no comments
Re: querying for text in an XML field         


Author: Jacob Sebastian
Date: Aug 19, 2008 00:15

you can use WITH XMLNAMESPACES to add namespace declarations. for example:

DECLARE @xml XML

SET @xml = '
http://xxx.org/schemas/03/2005/xxxx" Version="5.2.9">
balls
test

'

DECLARE @table TABLE( id INT IDENTITY PRIMARY KEY, your_xml XML )
INSERT INTO @table VALUES ( @xml )

;WITH XMLNAMESPACES(
default 'http://xxx.org/schemas/03/2005/xxxx'
)
SELECT
t.your_xml.value(
'(Publication/Identification/MediaType)[1]','VARCHAR(20)' )
FROM @table t
Show full article (2.08Kb)
no comments