• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 346
  • Last Modified:

SQL XML For XML

I need help creating a for xml path that looks like below...I have an outer node and then an inner node...See below for my desired output and regular SQL statement..<promotionInformation>
 is the outermost node and then <expandedPromotionsInformation> is the inner node..

<promotionInformation>
	  <expandedPromotionsInformation>		<promotionType>UserViewed_Value</promotionType>	<listingAddress>AddressLine1</listingAddress>		<promotionStartDate>DateStartPromotion</promotionStartDate>		<promotionEndDate>DateEndPromotion</promotionEndDate>
	</expandedPromotionsInformation>
 <summaryPromotionsInformation>
		<promotionType> UserViewed_Value </promotionType>
		<promotionEndDate> DateEndPromotion </promotionEndDate>
 </summaryPromotionsInformation>
</promotionInformation>



Select UserViewed_Value, AddressLine1 DateStartPromotion, DateEndPromotion
from Promotion

Open in new window

0
cheryl9063
Asked:
cheryl9063
  • 3
  • 2
1 Solution
 
cyberkiwiCommented:
Where does "summaryPromotionsInformation" fit?
0
 
cyberkiwiCommented:
If I read correctly, then

select
	UserViewed_Value 'expandedPromotionsInformation/promotionType',
	AddressLine1 'expandedPromotionsInformation/listingAddress',
	DateStartPromotion 'expandedPromotionsInformation/promotionStartDate',
	DateEndPromotion 'expandedPromotionsInformation/promotionEndDate',
	UserViewed_Value 'summaryPromotionsInformation/promotionType',
	DateEndPromotion 'summaryPromotionsInformation/promotionEndDate'
from Promotion
for xml path(''), root ('promotionInformation')

Open in new window

0
 
cheryl9063Author Commented:
I attached the real code below..

The result looks like this
<promotionInformation>
<expandedPromotionsInformation>
<UserViewed_Value>Open House</UserViewed_Value>
<promotionType>OpenHouse</promotionType>
<listingAddress>425 KENMORE Avenue</listingAddress>
</expandedPromotionsInformation>
</promotionInformation>

But I want this

<promotionInformation>
  <expandedPromotionsInformation>
    <promotionType>OpenHouse</promotionType>
    <listingAddress>425 KENMORE Avenue</listingAddress>
    <PromotionStartDate>null</PromotionStartDate>
    <PromotionEndDate>null</PromotionEndDate>
  </expandedPromotionsInformation>
 <SummaryPromotionsInformation>
      <promotionType>OpenHouse</promotionType>
       <PromotionEndDate>null</PromotionEndDate>
 </SummaryPromotionsInformation>
 </promotionInformation>       
Select pt.UserViewed_Value'expandedPromotionsInformation/UserViewed_Value',
pt.PromotionType'expandedPromotionsInformation/promotionType',
l.ListingAddressLine1'expandedPromotionsInformation/listingAddress',
DATENAME(w,lp.DateStartPromotion)+','+ DATENAME(month,lp.DateStartPromotion)+ ' ' + convert(varchar(25),DATEPART(MONTH,lp.DateStartPromotion))'expandedPromotionsInformation/promotionStartDate',
DATENAME(w,lp.DateEndPromotion)+','+ DATENAME(month,lp.DateEndPromotion)+ ' ' + convert(varchar(25),DATEPART(MONTH,lp.DateEndPromotion))'expandedPromotionsInformation/promotionEndDate'
from Listing l
join promotiontype pt on l.ListingID = pt.PromotionTypeID
join ListingPromotion lp on l.ListingID = lp.ListingPromotionID
 where l.listingID = 100000
for xml path(''), root ('promotionInformation')

Open in new window

0
 
cyberkiwiCommented:
First of all, in XML

    <PromotionStartDate>null</PromotionStartDate>
    <PromotionEndDate>null</PromotionEndDate>

That is not valid.  Those would be interpreted downstream as the STRING value NULL.  The database value null is equivalent to the XML empty node, which FOR XML strips out.

Secondly, it would save us both some time to post the original query in the first place.

Lastly, the only trick to making UserViewedValue in two different places is to use it twice in the SELECT clause, with a different path for each , as I have shown in my example.
0
 
cheryl9063Author Commented:
Thanks!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now