T-SQL字符串转换

时间:2014-09-15 15:21:33

标签: sql string tsql ini

我在SQL表中有一个SNMP消息列(格式化为VARCHAR(MAX)),如下所示。有没有办法将每个消息OID转换为列/值格式?

消息列内容示例:

community=PUBLIC, enterprise=1.1.1.1.1.1.1.1.1.1.1, uptime=42170345, agent_ip=1.1.1.1, version=Ver2, ...

期望的结果:

community    enterprise              uptime    agent_ip
---------    ----------              ------    --------
PUBLIC       1.1.1.1.1.1.1.1.1.1.1   42170345  1.1.1.1    ...

所以基本上需要将字符串拆分为","然后将INI值作为列返回。请注意,这是在一行(不创建或拆分为多行,只是多列)

这是SQL Server 2008 R2。

谢谢。

3 个答案:

答案 0 :(得分:0)

您可以在很多地方找到网络上的splitstring功能。以下是如何在查询中使用它来执行您想要的操作:

select t.*, cols.*
from table t cross apply
     (select max(case when token like 'community=%' then substring(token, 11, len(token))
                 end) as community,
             max(case when token like 'enterprise=%' then substring(token, 12, len(token))
                 end) as enterprise,
             max(case when token like 'uptime=%' then substring(token, 8, len(token))
                 end) as uptime,
             max(case when token like 'agent_ip=%' then substring(token, 10, len(token))
                 end) as agent_ip
      from dbo.SplitString(t.snmp, ',')(idx, token)
     ) cols;

答案 1 :(得分:0)

可能不是最有效的方法,但这有效:

SELECT
  REPLACE((SUBSTRING(MsgText,CHARINDEX('community=',MsgText),CHARINDEX(', enterprise=',MsgText) - CHARINDEX('community=',MsgText))),'community=','') AS community  
  ,REPLACE((SUBSTRING(MsgText,CHARINDEX('enterprise=',MsgText),CHARINDEX(', uptime=',MsgText) - CHARINDEX('enterprise=',MsgText))),'enterprise=','') AS enterprise
  ,REPLACE((SUBSTRING(MsgText,CHARINDEX('uptime=',MsgText),CHARINDEX(', agent_ip=',MsgText) - CHARINDEX('uptime=',MsgText))),'uptime=','') AS uptime
  ,REPLACE((SUBSTRING(MsgText,CHARINDEX('agent_ip=',MsgText),CHARINDEX(', version=',MsgText) - CHARINDEX('agent_ip=',MsgText))),'agent_ip=','') AS agent_ip
  ,MsgText
FROM Database.dbo.Table

如果有人需要一种解析SNMP消息的方法

答案 2 :(得分:0)

以下是使用将字符串转换为XML的解决方案,它为结果处理带来了更多自由:

-- Prepare data for solution testing
DECLARE @srctable TABLE (
    Id              INT,
    SnmpMessage     VARCHAR(MAX),
    SnmpMessageXml  XML
)

INSERT INTO @srctable
SELECT Id, SnmpMessage, SnmpMessageXml FROM ( VALUES
(1, 'community=PUBLIC, enterprise=1.1.1.1.1.1.1.1.1.1.1, uptime=42170345, agent_ip=1.1.1.1, version=Ver2',   null)
) v (Id, SnmpMessage, SnmpMessageXml)

-- Transform source formatted string to XML string
UPDATE @srctable
SET SnmpMessageXml = CAST('<row><data ' + REPLACE(REPLACE(SnmpMessage, ',', '"/><data '), '=', '="') + '"/></row>' AS XML)

-- Final select from XML data
SELECT  SnmpMessageXml.value('(/row/data/@community)[1]',  'VARCHAR(999)') AS community,
        SnmpMessageXml.value('(/row/data/@enterprise)[1]',  'VARCHAR(999)') AS enterprise,
        SnmpMessageXml.value('(/row/data/@uptime)[1]',  'VARCHAR(999)') AS uptime,
        SnmpMessageXml.value('(/row/data/@agent_ip)[1]',  'VARCHAR(999)') AS agent_ip,
        SnmpMessageXml.value('(/row/data/@version)[1]',  'VARCHAR(999)') AS version
FROM @srctable AS t