2014-10-30 89 views
0

我是這樣一個noob,對我來說,這看起來應該很簡單,但我已經掏出了我的最後一根頭髮,試圖找出它。SQL VIEW格式化XML字段類型

SQL視圖返回

  • Announcements
  • tp_ColumnSet
  • 類型XML

數據:

<datetime1>2014-11-06T08:00:00</datetime1> 
<ntext2>&lt;div class="ExternalClass92FCE11907AF41979E054BE04236CCBE"&gt;&lt;p&gt;Open Enrollment Meeting Wednesday, November 5th at 2pm. John Doe will be here to go over our 2014-2015 Medical/Dental &amp;amp; Life Insurance plans and our 401K Plan. We will also have a representative from Joe Medical joining us. Please plan to attend!&lt;/p&gt;&lt;/div&gt;</ntext2> 
<nvarchar1>Open Enrollment Meeting</nvarchar1> 

我我試圖在下午視圖

字段標題

公開註冊會議

場體

公開註冊會議週三,11月5日來顯示。 John Doe將會在這裏回顧我們2014-2015年的醫療/牙科& amp;人壽保險計劃和我們的401K計劃。我們也將有Joe Medical的代表加入我們。請計劃參加!

回答

0

您可以通過其value()方法從XML字段中提取節點。您基本上爲您感興趣的節點提供了一個XQuery,以及它應該返回的首選SQL類型。

select tp_ColumnSet.value('(/data/datetime1)[1]', 'datetime') as TheDate 
    , tp_ColumnSet.value('(/data/nvarchar1)[1]', 'nvarchar(200)') as TheTitle 
    , tp_ColumnSet.value('(/data/ntext2)[1]', 'nvarchar(max)') as TheBody 
from Announcements 

請注意,我還承擔了根節點是<data>,所以你可能需要調整代碼以適應。

這將返回:

2014-11-06 08:00:00.000 
Open Enrollment Meeting 
<div class="ExternalClass92FCE11907AF41979E054BE04236CCBE"><p>Open Enrollment Meeting Wednesday, November 5th at 2pm. John Doe will be here to go over our 2014-2015 Medical/Dental &amp; Life Insurance plans and our 401K Plan. We will also have a representative from Joe Medical joining us. Please plan to attend!</p></div> 
+0

只需要找到我的根節點現在我認爲。查詢正在運行NULL – SOCALNoob 2014-10-30 17:04:00

+0

您是否根據''上的註釋更新了XQuery以匹配您的XML結構?我的回答假設其' ... ... ...' – chrisb 2014-10-30 17:05:56

+0

謝謝....明白了,一旦我設置節點! – SOCALNoob 2014-10-30 17:11:04