Suppose my xml is stored in variable and I want to display xml data in tabular format. so this query i fired.
Declare @xml xml
set @Xml='<?xml version="1.0" encoding="utf-8"?>
<TickerBrokerStandardDateLineitem>
<Ticker />
<TickerID />
<TickerBrokerStandardDateLineitemValues>
<TickerBrokerStandardDateLineitemValue>
<TabName>xxxxxxxx</TabName>
<StandardDate>2010 FY</StandardDate>
<XFundCode>xxxxx</XFundCode>
<BRTab></BRTab>
<BRLineItem></BRLineItem>
<StandardLineItem>aaaaa</StandardLineItem>
<StandardValue>1608.7</StandardValue>
<ActualProvidedByCompany>NO</ActualProvidedByCompany>
</TickerBrokerStandardDateLineitemValue>
<TickerBrokerStandardDateLineitemValue>
<TabName>qqqqqqq</TabName>
<StandardDate>3Q 2018</StandardDate>
<XFundCode>RD_015</XFundCode>
<BRTab></BRTab>
<BRLineItem></BRLineItem>
<StandardLineItem>ssssss</StandardLineItem>
<StandardValue></StandardValue>
<ActualProvidedByCompany>YES</ActualProvidedByCompany>
</TickerBrokerStandardDateLineitemValue>
</TickerBrokerStandardDateLineitemValues>
</TickerBrokerStandardDateLineitem>'
SELECT
x.Rec.query('./TabName').value('.', 'VARCHAR(MAX)') AS 'TabName',
x.Rec.query('./StandardDate').value('.', 'VARCHAR(MAX)') AS 'StandardDate',
x.Rec.query('./XFundCode').value('.', 'VARCHAR(MAX)') AS 'XFundCode',
x.Rec.query('./BRTab').value('.', 'VARCHAR(MAX)') AS 'BRTab',
x.Rec.query('./BRLineItem').value('.', 'VARCHAR(MAX)') AS 'BRLineItem',
x.Rec.query('./StandardLineItem').value('.', 'VARCHAR(MAX)') AS 'StandardLineItem',
x.Rec.query('./StandardValue').value('.', 'DECIMAL (18, 2)') AS 'StandardValue',
x.Rec.query('./ActualProvidedByCompany').value('.', 'VARCHAR(MAX)') AS 'ActualProvidedByCompany'
FROM @xml.nodes('/TickerBrokerStandardDateLineitem/TickerBrokerStandardDateLineitemValues/TickerBrokerStandardDateLineitemValue') as x(Rec)
As you can see the xml attribute <StandardValue></StandardValue> has no value in xml, but it is declared as decimal. So what kind of change do I need to add in my code so, that when <StandardValue></StandardValue> has no data then query will return 0.00 as the default value?
What kind of change should be there in this line x.Rec.query('./StandardValue').value('.', 'DECIMAL (18, 2)') AS 'StandardValue',
I tried this nullif(0.0,x.Rec.query('./StandardValue').value('.', 'DECIMAL (18, 2)')) AS 'StandardValue', but no luck and Error I got Error converting data type nvarchar to numeric.
Please guide me. thanks