2016-01-22 43 views
0

你好人們的Stackoverflow 我有一個由自動生成的字符串組成的sql列。我需要從這個字符串中提取數字值以插入新列以供參考。從來沒有必要像這樣操縱一個字符串,但我真的很感謝我能得到任何幫助。 我做了一個我想要的excel模型 - 最左邊的「文本」列是我想要縮小到後面的列的那一列。注意:我已經把SQL'字符串,它們不是字符串的一部分:從字符串中提取多個元素

Text Column                                      ReserveFrom ReserveTo PaymentFrom paymentTo 
'Scheme ELS updated. Reserve Authority Level changed from: 11000000.00 to: 8000000.00.'                   11000000 8000000 
'Scheme CNST updated. Payment Authority Level changed from: 8000000.00 to: 10000000.00. Reserve Authority Level changed from: 8000000.00 to: 10000000.00.'  11000000 8000000  11000000  8000000 
'Scheme ELS updated. Payment Authority Level changed from: 350000.00 to: 100000.00. '                          350000  100000 

這些文本列的三個排列。

+0

能否請您發佈預期輸出 – TheGameiswar

+0

基於'charindex'值很多'substring'操縱一些'那裏像「%%」'的查詢結果欄是什麼,你應該尋找。 – beercodebeer

+0

你好。預期產出是四個coulumns:ReserveFrom,ReserveTo,PaymentFrom和PaymentTo。我需要這些列中的結果來自紅色的「文本列」cvalues。 – AJCT

回答

0
create table #text_table (text_field varchar(250)) 


insert into #text_table values 
('Scheme ELS updated. Reserve Authority Level changed from: 11000000.00 to: 8000000.00.'),  
('Scheme CNST updated. Payment Authority Level changed from: 8000000.00 to: 10000000.00. Reserve Authority Level changed from: 8000000.00 to: 10000000.00.'), 
('Scheme ELS updated. Payment Authority Level changed from: 350000.00 to: 100000.00.') 

drop table #text_table2 
select *, 
case when charindex('Reserve', text_field ,0) > 0 and charindex('Payment', text_field,0) = 0 then 'Reserve' 
    when charindex('Payment', text_field,0) > 0 and charindex('Reserve', text_field,0) = 0 then 'Payment' 
    when charindex('Payment', text_field,0) > 0 and charindex('Reserve', text_field,0) > 0 then 'Both' 
    else 'N/A' end as tag, 
charindex('from: ',text_field,0) as From_Index, 
charindex(' to: ',text_field, charindex('from: ',text_field,0)) as to_index, 
charindex('.',text_field, charindex(' to: ',text_field, charindex('from: ',text_field,0))) as dot_index 
into #text_table2 
from #text_table 

select * 
into #simple 
from #text_table2 where tag in ('Reserve','Payment') 

select 
substring(text_field, 0, charindex('.00. ',text_field,0)+3) as text_field 
into #parse 
from #text_table2 where tag = 'Both' 
union 
select 
substring(text_field, charindex('.00. ',text_field,0)+5, charindex('.00.',text_field,charindex('.00.',text_field,0)+3)) 
from #text_table2 where tag = 'Both' 

insert into #simple 
select *, 
case when charindex('Reserve', text_field ,0) > 0 and charindex('Payment', text_field,0) = 0 then 'Reserve' 
    when charindex('Payment', text_field,0) > 0 and charindex('Reserve', text_field,0) = 0 then 'Payment' 
    when charindex('Payment', text_field,0) > 0 and charindex('Reserve', text_field,0) > 0 then 'Both' 
    else 'N/A' end as tag, 
charindex('from: ',text_field,0) as From_Index, 
charindex(' to: ',text_field, charindex('from: ',text_field,0)) as to_index, 
charindex('.',text_field, charindex(' to: ',text_field, charindex('from: ',text_field,0))) as dot_index 
from #parse 




select *, 
substring(text_field, from_index+6, to_index - from_index - 6) as From_Value, 
substring(text_field, to_index+4, dot_index - to_index - 1) as To_Value 
from #simple 
+0

即將結束,應該讓@AJCT指向正確的方向,但不會考慮'CNST'計劃中'儲備'和'付款'的變化。 – beercodebeer

+0

查看標記字段 – Ben

+0

是的,它只爲'計劃CNST更新'標記'保留'。文本,當它明確指出「儲備」和「付款」都發生了變化。 – beercodebeer