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 2008 Forums
 Transact-SQL (2008)
 need help on XML

Author  Topic 

Idyana
Yak Posting Veteran

96 Posts

Posted - 2011-11-30 : 04:06:36
My query was fine as following,

declare @calGroup uniqueidentifier
select @calGroup=Newid()

declare
@data xml
set
@data='<data>
<currentSavingItem>
<whichMonth>Jan</whichMonth>
<acct1>100</acct1><acct2>10.00</acct2><acct3>10.00</acct3><acct4>10.00</acct4>
<acct5>10.00</acct5><acct6>10.00</acct6><acct7>10.00</acct7>
</currentSavingItem>
<currentSavingItem>
<whichMonth>Feb</whichMonth>
<acct1>16.00</acct1><acct2>16.00</acct2><acct3>16.00</acct3><acct4>16.00</acct4>
<acct5>10.00</acct5><acct6>10.00</acct6><acct7>10.00</acct7>
</currentSavingItem>
</data>'


select @calGroup,
a.b.value('whichMonth[1]','varchar(10)'), --as input1,
a.b.value('acct1[1]','decimal(10,2)') --as input2
from
@data.nodes('/data/currentSavingItem') a(b)

(No column name) (No column name) (No column name)
952C7140-1616-4B9E-A0D2-C36E054480E2 Jan 100.00
952C7140-1616-4B9E-A0D2-C36E054480E2 Feb 16.00


But,

declare @calGroup uniqueidentifier
select @calGroup=Newid()

declare
@data xml
set
@data='<data>
<currentSavingItem>
<whichMonth>Jan</whichMonth>
<acct1></acct1><acct2>10.00</acct2><acct3>10.00</acct3><acct4>10.00</acct4>
<acct5>10.00</acct5><acct6>10.00</acct6><acct7>10.00</acct7>
</currentSavingItem>
<currentSavingItem>
<whichMonth>Feb</whichMonth>
<acct1>16.00</acct1><acct2>16.00</acct2><acct3>16.00</acct3><acct4>16.00</acct4>
<acct5>10.00</acct5><acct6>10.00</acct6><acct7>10.00</acct7>
</currentSavingItem>
</data>'

select @calGroup,
a.b.value('whichMonth[1]','varchar(10)'), --as input1,
a.b.value('acct1[1]','decimal(10,2)') --as input2
from
@data.nodes('/data/currentSavingItem') a(b)



which is <acct1></acct1>, it's return an error as following
Error converting data type nvarchar to numeric.

How to make it as following,
if <acct1></acct1>, it will convert to 0.00

I'm stuck

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-11-30 : 07:07:27
Check if it is blank before doing the conversion - as in:

--a.b.value('acct1[1]','decimal(10,2)') --as input2
CAST(NULLIF(a.b.value('acct1[1]','varchar(32)'),'') AS DECIMAL(10,2)) --as input2
Go to Top of Page

Idyana
Yak Posting Veteran

96 Posts

Posted - 2011-12-03 : 06:22:35
tq sir
Go to Top of Page
   

- Advertisement -