How to get exact number from text field in sql server table -
i have 1 table including customer_contact_no, name, income. want customer net income value income column.
final output should below. there 100000+ rows in table.
af-4838 - mr.gayan doctor , salary- rs.95000/-. has coconut land , monthly income rs.150000/-. expenses – rs.55000 , net income – rs.190000/-
av-7392 - monthly net income 55000/-
af-3746 - wife’s salary –rs 25000, shop owner , monthly income shop = rs 100000/- , net income month rs. 80000/- after expenses of rs.45000
af-6453 - total monthly net income 60000/
declare @test table ( val varchar(300) ) insert @test select 'blah blah blah net income blah blah 86000' insert @test select 'blah blah blah net income blah blah 4000, expenses 0' insert @test select 'blah blah blah net income blah blah 80,000 g' insert @test select 'blah expenses 60 blah blah net income blah blah 6000 blah ' insert @test select 'blah net income whatever 6000. blah blah net income blah blah 86000' select replace(substring(val,0, case when patindex('%[^0-9,]%',val) = 0 len(val) + 1 else patindex('%[^0-9,]%',val) end),',','') netincomes ( select substring(val, inc, len(val) - inc + 1) val ( select substring(val,net,len(val) - net + 1) val, patindex('%[0-9]%',substring(val,net,len(val) - net + 1)) inc ( select val, len(val) - charindex('emocni ten', reverse(val)) net @test )q )q2 )q3
you cte's if prefer
declare @test table ( val varchar(300) ) insert @test select 'blah blah blah net income blah blah 86000' insert @test select 'blah blah blah net income blah blah 4000, expenses 0' insert @test select 'blah blah blah net income blah blah 80,000 g' insert @test select 'blah expenses 60 blah blah net income blah blah 6000 blah ' insert @test select 'blah net income whatever 6000. blah blah net income blah blah 86000' ;with findnetincomestring (select val, len(val) - charindex('emocni ten', reverse(val)) net @test), findfirstnumeric (select substring(val,net,len(val) - net + 1) val, patindex('%[0-9]%',substring(val,net,len(val) - net + 1)) inc findnetincomestring), getnumericonly (select substring(val, inc, len(val) - inc + 1) val findfirstnumeric), getnetincome (select replace(substring(val,0, case when patindex('%[^0-9,]%',val) = 0 len(val) + 1 else patindex('%[^0-9,]%',val) end),',','') netincomes getnumericonly) select netincomes getnetincome;
result
netincomes ------ 86000 4000 80000 6000 86000
test non-cte: http://rextester.com/lgfw6563
test cte: http://rextester.com/srtg30852
Comments
Post a Comment