Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 need empty xml tags to be NULL

Author  Topic 

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-12-07 : 18:12:20
Hi there

DECLARE @xmlDoc NVARCHAR(MAX)
DECLARE @handle INT

SET @xmlDoc =
'<?xml version="1.0" encoding="utf-16"?>
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<FileInfo>
<Copyright>Me</Copyright>
</FileInfo>
<Names>
<Name>
<NameID/>
<Type>G</Type>
<Name>Special Bear</Name>
</Name>
</Names>
</Data>'

EXEC sp_xml_preparedocument @handle OUTPUT, @xmlDoc

-- Execute a SELECT statement that uses the OPENXML rowset provider.
--INSERT INTO dbo.stgArtists
SELECT NameID ,
Type ,
Name
FROM OPENXML (@handle, '/Data/Names/Name',2)
WITH (NameID varchar(255),
[Type] varchar(255),
[Name] varchar(255))

EXEC sp_xml_removedocument @handle


returns the following result

NameID Type name
G Special Bear


I would like NameID to be NULL. How do I go about doing that?

Thanks


If you don't have the passion to help people, you have no passion

robvolk
Most Valuable Yak

15732 Posts

Posted - 2010-12-07 : 18:14:25
SELECT NullIf(NameID,'') NameID ,
Type ,
Name
FROM OPENXML (@handle, '/Data/Names/Name',2)
WITH (NameID varchar(255),
[Type] varchar(255),
[Name] varchar(255))
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-12-07 : 18:59:21
Thanks!

This also worked


SELECT NameID ,
Type ,
Name
FROM OPENXML (@handle, '/Data/Names/Name',3)
WITH (NameID varchar(255) 'text()',
[Type] varchar(255),
[Name] varchar(255))



If you don't have the passion to help people, you have no passion
Go to Top of Page
   

- Advertisement -