SQL字符串值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL字符串值相关的知识,希望对你有一定的参考价值。

有一个大的XML文件存储在一个表内的字段中,许多值已经被提取并存储在表中,但我希望捕获(2)额外的:账户类型="当前 "状态="X "和账户类型="以前 "状态="Y"。 在下面的部分输出中,没有前账户类型,所以我需要一个策略来缺失。

    <ncf_report xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://cp.com/rules/client">
  <admin>
    <product_reference>12345678901234</product_reference>
    <report_type>XXXXXXX</report_type>
    <status>XXXXXXXX</status>
    <ownership>XXXXXXX</ownership>
    <report_code>1234</report_code>
    <report_description>XXXXXXXXXXXXXXXXX</report_description>
    <purpose>XXXXXXXX</purpose>
    <date_request_ordered>mm/dd/yyyy</date_request_ordered>
    <date_request_received>mm/dd/yyyy</date_request_received>
    <date_request_completed>mm/dd/yyyy</date_request_completed>
    <time_report_processed>01234</time_report_processed>
    <multiple_scores_ordered>false</multiple_scores_ordered>
    <vendor name="XXXXXXXXXXXXX" address="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" />
    <report>
      <sequence>0000000000</sequence>
      <count>0000000000</count>
    </report>
  </admin>
  <report>
    <alerts_scoring>
      <scoring>
        <score status="XXXXXXXXXX">
          <model_label>XXXXXXXXXXXXXXXXX</model_label>
          <score>123</score>
          <rating_state>XX</rating_state>
          <classification>XXXXXXXXXXXXXXXXX</classification>
          <reason_codes>
            <code>05</code>
            <description>XXXXXXXXXXXXXXXXX</description>
          </reason_codes>
          <reason_codes>
            <code>04</code>
            <description>XXXXXXXXXXXXXXXXX</description>
          </reason_codes>
          <reason_codes>
            <code>10</code>
            <description>XXXXXXXXXXXXXXXXX</description>
          </reason_codes>
          <reason_codes>
            <code>27</code>
            <description>XXXXXXXXXXXXXXXXX</description>
          </reason_codes>
        </score>
      </scoring>
      <general>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</general>
      <general>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</general>
      <general>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</general>
    </alerts_scoring>
    <vendor_dataset>
      <subjects>
        <subject type="Primary" relationship_to_data="Subject">
          <name type="Report Subject">
            <first>XXXX</first>
            <middle>X</middle>
            <last>XXXX</last>
          </name>
          <name type="Alias">
            <first>XXXXXXXXXX</first>
            <last>XXXXXXXX</last>
          </name>
          <birth_date>mm/dd/yyyy</birth_date>
          <ssn>999999999</ssn>
          <address type="residence" ref="1" />
          <address type="former" ref="2" />
          <address type="former" ref="3" />
        </subject>
      </subjects>
      <addresses>
        <address id="1">
          <house>1234</house>
          <street1>sample</street1>
          <city>sample</city>
          <state>XX</state>
          <postalcode>12345</postalcode>
          <zip4>1234</zip4>
          <date_first_at_address>mm/dd/yyyy</date_first_at_address>
          <date_last_at_address>mm/dd/yyyy</date_last_at_address>
        </address>
        <address id="X">
          <house>1234</house>
          <street1>XXXXXXXXX</street1>
          <city>XXXXXXXXX</city>
          <state>XX</state>
          <postalcode>12345</postalcode>
          <zip4>1234</zip4>
          <date_first_at_address>mm/dd/yyyy</date_first_at_address>
          <date_last_at_address>mm/dd/yyyy</date_last_at_address>
        </address>
       </addresses>
    </vendor_dataset>
    <summary>
      <date_oldest_trade>mm/dd/yyyy</date_oldest_trade>
      <date_latest_trade>mm/dd/yyyy</date_latest_trade>
      <date_latest_activity>mm/dd/yyyy</date_latest_activity>
      <includes_bankruptcies flag="false" />
      <includes_other_records public_records="false" collection="false" consumer_statement="false" />
      <credit_range high="12345" low="123" number_trade_lines="123" />
      <account_status_counters>
       <!-- here --> <account type="current" description="Pays Account as Agreed" status="1">12</account>
      </account_status_counters>
      <account_summaries>
        <account type="Open-ended">
          <number_accounts>0</number_accounts>
          <total_owed>0</total_owed>
          <total_past_due>0</total_past_due>
          <high_amount>0</high_amount>
        </account>
        <account type="Revolving">
          <number_accounts>00</number_accounts>
          <total_owed>1234</total_owed>
          <total_past_due>0</total_past_due>
          <high_amount>12345</high_amount>
        </account>
        <account type="Installment">
          <number_accounts>00</number_accounts>
          <total_owed>12345</total_owed>
          <total_past_due>0</total_past_due>
          <high_amount>123456</high_amount>
        </account>
      </account_summaries>
      <inquiry_history count="0" />
    </summary>
    <employment_history>
      <employment_primary_subject>
        <job entry="current" indirectly_verified="false">
          <employer>
            <name>XXXXXXXXXXX</name>
          </employer>
        </job>
        <job entry="first_former" indirectly_verified="false">
          <employer>
            <name>XXXXXXXX</name>
            <city>XXXXXXX</city>
            <state>XX</state>
          </employer>
        </job>
      </employment_primary_subject>
    </employment_history>
    <trade_account_activity>
      <credit_trades>
        <credit_trade automated_tape_supplier="false">
          <reporting_member>
            <number>1234X1234</number>
            <name>XXX/1234</name>
          </reporting_member>
          <account>
            <type>XXXXXXXXX</type>
            <terms>XXX</terms>
            <months_reviewed>00</months_reviewed>
            <designator>XXXXXXXX(XXXXX)</designator>
          </account>
          <date_reported>mm/dd/yyyy</date_reported>
          <date_opened>mm/dd/yyyy</date_opened>
          <date_last_activity>mm/dd/yyyy</date_last_activity>
          <current_rate>XXXXXXXXXXXXXXXXX</current_rate>
          <highest_amount>1234</highest_amount>
          <balance_amount>00</balance_amount>
          <past_due_amount>00</past_due_amount>
          <messages>
            <message code="XX">XXXXXXXXXXXXXX</message>
            <message code="XX">XXXXXXXXXXXXXX</message>
          </messages>
        </credit_trade>        
          </account>
          <date_reported>mm/dd/yyyy</date_reported>
          <date_opened>mm/dd/yyyy</date_opened>
          <date_last_activity>mm/dd/yyyy</date_last_activity>
          <current_rate>XXXXXXXXXXXXXXXXXXXXXX</current_rate>
          <highest_amount>123456</highest_amount>
          <balance_amount>123456</balance_amount>
          <past_due_amount>0</past_due_amount>
          <messages>
            <message code="XX">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</message>
          </messages>
        </credit_trade>
      </credit_trades>
    </trade_account_activity>
    <inquiry_history>
      <inquiry date="mm/dd/yyyy" name="XXXXXXXXXXXXXXXXXXXXXXX" member="12345X1234" />
      <inquiry date="mm/dd/yyyy" name="XXXXXXXXXXXXXXXXXXXXXXX" member="12345Y1234" />
      <inquiry date="mm/dd/yyyy" name="CXXXXXXXXXXXXXXXXXXXXXXX" member="12345Z1234" />
      <inquiry date="mm/dd/yyyy" name="XXXXXXXXXXXXXXXXXXXXXXX &amp; X" member="12345W1234" />
      <inquiry date="mm/dd/yyyy" name="XXXXXXXXXXXXXXXXXXXXXXX" member="12345V1234" />
      <inquiry date="mm/dd/yyyy" name="XXXXXXXXXXXXXXXXXXXXXXX" member="12345U1234" />
      <inquiry date="mm/dd/yyyy" name="XXXXXXXXXXXXXXXXXXXXXXX" member="12345T1234" />
    </inquiry_history>
  </report>
</ncf_report>

我希望从账户类型="当前 "状态="X "中提取X值,如果存在账户类型="前任",则提取Y值。 在这种情况下,该值1.添加到XML中,以突出感兴趣的区域。我首先将数据集对下到一个临时表中。

select id,       
       LEFT(SUBSTRING(CreditscoreXML,charindex('<account type="current"',CreditscoreXML),charindex('</account>',CreditscoreXML)),charindex('">',SUBSTRING(CreditscoreXML,charindex('<account type="current"',CreditscoreXML),charindex('</account>',CreditscoreXML)))) [Current_Status]
select 
  Current_Status, --just so I see output is correct in temp table
  substring(Current_Status, charindex('status="', 
    Current_Status)+8,len(Current_Status)-charindex('status',Current_Status)) [Current_Status]
from #TempCurrent 

从这里我进一步尝试细化文本搜索。 试图找出如何消除1后面的 "或更好的解决方案,以提取当前和以前的状态,前者可以缺少,需要这个由Id分组。

当前输出

Current_Worse_Score    Current_Worse_Score      Former_Worse
Original Text           1"
答案

与其操作字符串数据,不如尝试使用SQL Server中内置的XML函数,让你的生活更轻松。例如,在SQL Server中使用XML函数

create table dbo.Foo (
  id int not null,
  bar xml not null
);

insert dbo.Foo (id, bar) values (47, N'<ncf_report xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://cp.com/rules/client">
  <!-- the rest of your xml... -->
</ncf_report>')

;with xmlnamespaces(default 'http://cp.com/rules/client')
select
  id,
  x.a.value(N'@description', N'nvarchar(50)') as [Description],
  x.a.value(N'@status', N'nvarchar(50)') as [Status],
  x.a.value(N'.', N'nvarchar(50)') as [Account]
from dbo.Foo
cross apply bar.nodes(N'/ncf_report/report/summary/account_status_counters/account[@type="current"]') x(a)

其结果是...

id  Description             Status  Account
47  Pays Account as Agreed  1       12
另一答案

你可以使用内置的XML数据类型方法,从存储为XMLType列的XML实例中查询所需值。

DECLARE @X XML;
SET @X = '<ncf_report xmlns:xsd="http://www.w3.org/2001/XMLSchema" ....>'; -- provided XML instance
CREATE TABLE NCFREPORT  
(  
    ncfreportcol XML NOT NULL   
);

INSERT INTO ncfreport (ncfreportcol) values (@X); -- inserting the XML instance stored temporarily in above variable X

WITH xmlnamespaces ('http://cp.com/rules/client' as NR)
SELECT  T.acc.value('(@status)[1]', 'int') AS Status,
        T.acc.value('(@type)[1]', 'varchar(20)') AS AccType,
        T.acc.value('(text())[1]', 'int') AS Acc
FROM ncfreport cross apply ncfreport.ncfreportcol.nodes ('/NR:ncf_report/NR:report/NR:summary/NR:account_status_counters/NR:account') as t(acc);

这将导致以下输出。

Status  AccType Acc
1       current 12

如果你在XML实例中定义了多个账户标签,它将在输出中为每个账户产生一行。我还注意到,在上面的XML片段中缺少了开头或结尾标签。在输入表格之前,最好也看一下对XML的验证。请看一下这里的各种XML数据类型方法--。https:/docs.microsoft.comen-ussqlt-sqlxmlxml-data-type-methods?view=sql-server-ver15。

以上是关于SQL字符串值的主要内容,如果未能解决你的问题,请参考以下文章

MYSQL中使用sql函数怎样截取字符串

SQL - 查询字符串是不是包含列中的部分值

SQL 从字符串中提取值

在 SQL 中对日期的字符串值进行排序

SQL:使用分隔符值拆分字符串

PL/SQL 创建返回两个字符串值的过程