将单个字段分隔/拆分为多个字段

时间:2018-08-22 23:56:04

标签: sql-server

我有一个表'Waterwatch_Active',其中有一个字段'Att'。

Att字段是一个复杂的串联字段(实际上看起来像html),我需要拆分为以下内容:

  • site_name
  • site_code

这是我的表Waterwatch_Active Waterwatch_active example

的Att字段的示例
textattributes>\n  <li><strong><span class="atr-name">site_name</span>:</strong> <span class="atr-value">Walkers Lake</span></li>\n  <li><strong><span class="atr-name">site_code</span>:</strong> <span class="atr-value">NC_LWA001</span></li>\n  \n  \n  <li><strong><span class="atr-name">basin_name</span>:</strong> <span class="atr-value">Avoca River</span></li>\n  \n  \n  \n  <li><strong><span class="atr-name">water_type</span>:</strong> <span class="atr-value">Other</span></li>\n  <li><strong><span class="atr-name">lng</span>:</strong> <span class="atr-value">142.923793</span></li>\n  <li><strong><span class="atr-name">lat</span>:</strong> <span class="atr-value">-36.540724</span></li>\n  <li><strong><span class="atr-name">zone</span>:</strong> <span class="atr-value">54</span></li>\n  \n  \n  <li><strong><span class="atr-name">public_url</span>:</strong> <span class="atr-value">http://www.vic.waterwatch.org.au/site/170005</span></li>\n</ul>

textattributes>\n  <li><strong><span class="atr-name">site_name</span>:</strong> <span class="atr-value">Murray River behind Koondrook PS</span></li>\n  <li><strong><span class="atr-name">site_code</span>:</strong> <span class="atr-value">NC_MUR400</span></li>\n  \n  \n  <li><strong><span class="atr-name">basin_name</span>:</strong> <span class="atr-value">Loddon River</span></li>\n  <li><strong><span class="atr-name">catchment_name</span>:</strong> <span class="atr-value">Loddon River</span></li>\n  <li><strong><span class="atr-name">sub_catchment_name</span>:</strong> <span class="atr-value">Loddon River</span></li>\n  <li><strong><span class="atr-name">reach_name</span>:</strong> <span class="atr-value">7_38 Gunbower Creek</span></li>\n  <li><strong><span class="atr-name">water_type</span>:</strong> <span class="atr-value">Other</span></li>\n  <li><strong><span class="atr-name">lng</span>:</strong> <span class="atr-value">144.130368</span></li>\n  <li><strong><span class="atr-name">lat</span>:</strong> <span class="atr-value">-35.633233</span></li>\n  <li><strong><span class="atr-name">zone</span>:</strong> <span class="atr-value">55</span></li>\n  \n  \n  <li><strong><span class="atr-name">public_url</span>:</strong> <span class="atr-value">http://www.vic.waterwatch.org.au/site/170021</span></li>\n</ul>

我已经弄清楚了如何一次选择一个,但是我需要一次全部选择并生成一个结果表。这就是我设法使site_name消失的方法:

--site_name
DECLARE @SiteNamekeysValueToSearch NVARCHAR(4000) = '"atr-value">'
DECLARE @SiteNameuntilThisCharAppears NVARCHAR(4000) = '</span>'
DECLARE @SiteNamekeysValueToSearchPattern NVARCHAR(4000) = '%' + @SiteNamekeysValueToSearch + '%'

SELECT SUBSTRING(
           Att,
           PATINDEX(@SiteNamekeysValueToSearchPattern, Att) + LEN(@SiteNamekeysValueToSearch),
           CHARINDEX(
               @SiteNameuntilThisCharAppears,
               Att,
               PATINDEX(@SiteNamekeysValueToSearchPattern, Att) + LEN(@SiteNamekeysValueToSearch)
           ) -(PATINDEX(@SiteNamekeysValueToSearchPattern, Att) + LEN(@SiteNamekeysValueToSearch))
       )
FROM Waterwatch_Active
--End site_name

所以理想情况下,我希望我的结果表看起来像这样 Waterwatch_Active result

+----------------------------------+-----------+
|            Site_Name             | Site_Code |
+----------------------------------+-----------+
| Walkers Lake                     | NC_LWA001 |
| Murray River behind Koondrook PS | NC_MUR400 |
+----------------------------------+-----------+

请提供任何帮助,我们将不胜感激,但我决心不放弃。

1 个答案:

答案 0 :(得分:0)

假设您正在逐行处理此文件,则可以使用以下命令获取site_name。您可以对其进行修改以获取其他属性。

代码将字符串转换为有效的xml字符串(它需要在前端使用<ul>,然后使用xpath查询来获取site_name的值:

用您的字符串(您的字符串)代替(几乎)xml字符串。

declare @xml xml
set @xml=convert(xml,'<ul>'+'*your string*')
select @xml.value('(/ul/li/strong/span[text()="site_name"]/../../span)[1]','varchar(100)')

如果要对表执行此操作:

select convert(xml,'<ul>'+att).value('(/ul/li/strong/span[text()="site_name"]/../../span)[1]','varchar(100)') as SiteName
from waterwatch_active

最后,如果您要处理多个列,例如site_name和site_code,请执行以下操作:

;with attTable as (select convert(xml,'<ul>'+att) as attXML from waterwatch_active)
select attXML.value('(/ul/li/strong/span[text()="site_name"]/../../span)[1]','varchar(100)') as SiteName,
attXML.value('(/ul/li/strong/span[text()="site_code"]/../../span)[1]','varchar(100)') as SiteCode
from attTable