我有一个表,包括Customer_Contact_No,Name,Income。我希望将客户净收入作为收入列的值。
AF-4838 - 加扬先生是博士和他的薪水 - RS.95000 / - 。他有一块椰子土地,月收入为150000卢比/ - 。他的费用--55000卢比和他的净收入--Rs.190000 / -
AV-7392 - 每月净收入为55000 / -
AF-3746 - 妻子的工资-25,000,他是店主,商店的月收入= 100000卢比/ - 当月的净收入是卢比。 80000 / - 费用为45,000卢比
AF-6453 - 每月总收入为60000 /
答案 0 :(得分:0)
您可以使用嵌套的CHARINDEX函数获取子字符串起点后的第一个数字值“净收入”。
答案 1 :(得分:0)
UDF将根据两个非类似分隔符
提取字符串您可能会注意到我将“净收入”替换为“||净收入”我这样做是为了让我的第一个分隔符为“|||”并且还为我的第二个分隔符“/”在字符串的末尾添加了一个“/”。这可以确保字符串至少有一个“/”
示例强>
Select A.customer_contact_no
,Income = try_convert(int,right(B.RetVal,charindex(' ',replace(reverse(RetVal),'.',' '))-1))
From YourTable A
Cross Apply [dbo].[udf-Str-Extract](replace([Income],'Net Income','|||Net Income')+'/','|||','/') B
Where RetVal Like 'Net Income%'
<强>返回强>
感兴趣的UDF
CREATE FUNCTION [dbo].[udf-Str-Extract] (@String varchar(max),@Delimiter1 varchar(100),@Delimiter2 varchar(100))
Returns Table
As
Return (
with cte1(N) As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
cte2(N) As (Select Top (IsNull(DataLength(@String),0)) Row_Number() over (Order By (Select NULL)) From (Select N=1 From cte1 N1,cte1 N2,cte1 N3,cte1 N4,cte1 N5,cte1 N6) A ),
cte3(N) As (Select 1 Union All Select t.N+DataLength(@Delimiter1) From cte2 t Where Substring(@String,t.N,DataLength(@Delimiter1)) = @Delimiter1),
cte4(N,L) As (Select S.N,IsNull(NullIf(CharIndex(@Delimiter1,@String,s.N),0)-S.N,8000) From cte3 S)
Select RetSeq = Row_Number() over (Order By N)
,RetPos = N
,RetVal = left(RetVal,charindex(@Delimiter2,RetVal)-1)
From (
Select *,RetVal = Substring(@String, N, L)
From cte4
) A
Where charindex(@Delimiter2,RetVal)>1
)
/*
Max Length of String 1MM characters
Declare @String varchar(max) = 'Dear [[FirstName]] [[LastName]], ...'
Select * From [dbo].[udf-Str-Extract] (@String,'[[',']]')
*/
答案 2 :(得分:0)
declare @test table
(
val varchar(300)
)
INSERT INTO @test
select 'blah blah blah net income blah blah 86000'
INSERT INTO @test
select 'blah blah blah net income blah blah 4000, expenses 0'
INSERT INTO @test
select 'blah blah blah net income blah blah 80,000 g'
INSERT INTO @test
select 'blah expenses are 60 blah blah net income blah blah 6000 blah '
INSERT INTO @test
select 'blah net income for whatever 6000. blah blah net income blah blah 86000'
select replace(substring(val,0, CASE WHEN PATINDEX('%[^0-9,]%',val) = 0 THEN len(val) + 1 ELSE PATINDEX('%[^0-9,]%',val) END),',','') as NetIncomes
from
(
select substring(val, inc, len(val) - inc + 1) as val
from
(
select substring(val,net,len(val) - net + 1) as val, PATINDEX('%[0-9]%',substring(val,net,len(val) - net + 1)) as inc
from
(
select val, len(val) - charindex('emocni ten', reverse(val)) as net
from @test
)q
)q2
)q3
如果您愿意,也可以使用CTE执行此操作
declare @test table
(
val varchar(300)
)
INSERT INTO @test
select 'blah blah blah net income blah blah 86000'
INSERT INTO @test
select 'blah blah blah net income blah blah 4000, expenses 0'
INSERT INTO @test
select 'blah blah blah net income blah blah 80,000 g'
INSERT INTO @test
select 'blah expenses are 60 blah blah net income blah blah 6000 blah '
INSERT INTO @test
select 'blah net income for whatever 6000. blah blah net income blah blah 86000'
;WITH FindNetIncomeString AS (select val, len(val) - charindex('emocni ten', reverse(val)) as net from @test),
FindFirstNumeric AS (select substring(val,net,len(val) - net + 1) as val, PATINDEX('%[0-9]%',substring(val,net,len(val) - net + 1)) as inc from FindNetIncomeString),
GetNumericOnly AS (select substring(val, inc, len(val) - inc + 1) as val from FindFirstNumeric),
GetNetIncome as (select replace(substring(val,0, CASE WHEN PATINDEX('%[^0-9,]%',val) = 0 THEN len(val) + 1 ELSE PATINDEX('%[^0-9,]%',val) END),',','') as NetIncomes from GetNumericOnly)
select NetIncomes
from GetNetIncome;
结果
NetIncomes
------
86000
4000
80000
6000
86000
使用非CTE进行测试:http://rextester.com/LGFW6563
使用CTE进行测试:http://rextester.com/SRTG30852