如何在sql中拆分字符串?

时间:2016-10-11 10:51:45

标签: sql sql-server

我有两张桌子,其中一张我有这样的数据:

id  description
2   12.07.13y 1000eur to bank account KZ21321o0002134   
4   To bank account KZasd9093636 12 of May 2016y 200dusd

我还有第二个表,我需要先从表中输入过滤后的信息,如:

  1. ID
  2. 数据
  3. BANK_ACCOUNT
  4. tranfered_money
  5. 首先我需要拆分描述,然后我需要识别ban_account,它始终以" KZ",data和transfered_money

    开头

2 个答案:

答案 0 :(得分:0)

这很糟糕但似乎能够提取ban_acount:

CREATE TABLE exp
(
 column1 varchar(400)
);

Insert into exp (column1) values ('12.07.13y 1000eur to bank account KZ21321o0002134');
Insert into exp (column1) values ('To bank account KZasd9093636 12 of May 2016y 200dusd');

Select
CASE
  WHEN CHARINDEX ( SPACE(1), SUBSTRING ( column1, CHARINDEX('KZ' , column1),LEN(column1))) = 0
  THEN SUBSTRING ( column1, CHARINDEX('KZ' , column1),LEN(column1))
  ELSE SUBSTRING ( SUBSTRING (column1, CHARINDEX('KZ' , column1),LEN(column1)), 0, CHARINDEX (SPACE(1), SUBSTRING(column1, CHARINDEX('KZ' , column1),LEN(column1))))
END result
From exp

答案 1 :(得分:0)

首先将表格转换为XML。

然后创建包含月/周日名称和数字1到3000的表(或者您可以将2016作为当前年份)

您需要一张带货币的桌子。我根据here的数据制作了一个。

DECLARE @x xml

;WITH YourTable AS (  --I use this CTE, you should use your table in scripts below
SELECT *
FROM (VALUES
(2,   '12.07.13y 1000eur to bank account KZ21321o0002134'),
(4,   'To bank account KZasd9093636 12 of May 2016y 200dusd')
) as t(id, [description])
)

SELECT @x = (  --XML sample that we get you can see below after output
    SELECT CAST(N'<row id="'+CAST(id as nvarchar(max))+'"><b>'+REPLACE([description],' ','</b><b>')+'</b></row>' as xml)
    FROM YourTable
    FOR XML PATH('')
)
;WITH CurrencyList AS ( --Currency table
SELECT *
FROM (VALUES
('AED', 'United Arab Emirates Dirham'),
('AFN', 'Afghanistan Afghani'),
('ALL', 'Albania Lek'),
('AMD', 'Armenia Dram'),
...
('ZAR', 'South Africa Rand'),
('ZMW', 'Zambia Kwacha'),
('ZWD', 'Zimbabwe Dollar')
) as t(code, countryname)
),cte AS (  --generate numbers 1 to 3000
SELECT 0 as d
UNION ALL
SELECT d+1
FROM cte 
WHERE d < 3000
), datenames AS ( --generate datenames
SELECT  d,
        CASE WHEN d < 7 THEN DATENAME(weekday,DATEADD(day,d,'1970-01-01 00:00:00.000')) ELSE NULL END as weekday_name,
        CASE WHEN d < 12 THEN DATENAME(month,DATEADD(month,d,'1970-01-01 00:00:00.000')) ELSE NULL END as mon_name
FROM cte
)

--Final query
SELECT  t.c.value('../@id','int') as id,
        t.c.value('.','nvarchar(max)') as str_part,
        CASE WHEN t.c.value('.','nvarchar(max)') LIKE 'KZ%' THEN 'bank_account' 
                WHEN countryname IS NOT NULL THEN 'tranfered_money'
                WHEN dn.d IS NOT NULL OR RIGHT(t.c.value('.','nvarchar(max)'),1) ='y' THEN 'datepart'
                ELSE NULL END as what_is
FROM @x.nodes('/row/b') as t(c)
LEFT JOIN CurrencyList cl
    ON RIGHT(t.c.value('.','nvarchar(max)'),3) = cl.code --check 3 last symbols of string with currency codes
LEFT JOIN datenames dn
    ON  dn.d = t.c.value('. cast as xs:int?','int')  -- if it is a day/month/year number
        OR t.c.value('.','nvarchar(max)') = dn.weekday_name -- or it is a week day name
        OR t.c.value('.','nvarchar(max)') = dn.mon_name --or month name
OPTION (MAXRECURSION 0)

会带给你:

id  str_part        what_is
2   12.07.13y       datepart
2   1000eur         tranfered_money
2   to              NULL
2   bank            NULL
2   account         NULL
2   KZ21321o0002134 bank_account
4   To              NULL
4   bank            NULL
4   account         NULL
4   KZasd9093636    bank_account
4   12              datepart
4   of              NULL
4   May             datepart
4   2016y           datepart
4   200dusd         tranfered_money

之后,您需要以正常日期形式提供日期,这就是全部。

XML示例:

<row id="2">
  <b>12.07.13y</b>
  <b>1000eur</b>
  <b>to</b>
  <b>bank</b>
  <b>account</b>
  <b>KZ21321o0002134</b>
</row>
<row id="4">
  <b>To</b>
  <b>bank</b>
  <b>account</b>
  <b>KZasd9093636</b>
  <b>12</b>
  <b>of</b>
  <b>May</b>
  <b>2016y</b>
  <b>200dusd</b>
</row>