简体   繁体   English

T-SQL字符串转换

[英]T-SQL string conversion

I have an SNMP message column (formatted as VARCHAR(MAX)) in a SQL table like the one below. 我在SQL表中有一个SNMP消息列(格式为VARCHAR(MAX)),如下所示。 Is there a way to convert each message OID into a column/value format? 有没有一种方法可以将每个消息OID转换为列/值格式?

Message column content sample: 消息列内容示例:

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

Desired result: 所需结果:

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

So basically it would need to split the string by ", " and then return INI values as columns. 因此,基本上它需要将字符串用“,”分割,然后将INI值作为列返回。 Note this is on one row (not creating or splitting to multiple rows, just multiple columns) 请注意,这是在一行上(不是创建或拆分为多行,而是多列)

This is SQL Server 2008 R2. 这是SQL Server 2008 R2。

Thank you. 谢谢。

You can find a splitstring function on the web in many places. 您可以在网上许多地方找到splitstring函数。 Here is how you would use it in a query to do what you want: 这是在查询中使用它来执行所需操作的方式:

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;

Probably not the most efficient way to do this, but this works: 可能不是执行此操作的最有效方法,但这可行:

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

In case anyone needs a method to parse SNMP messages 万一有人需要解析SNMP消息的方法

Here is solution using transforming string to XML which brings more freedom with result processing: 这是使用将字符串转换为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM