我有SQL Server表调用Client_details,并且有一个名为Client income的列。它是一个文本字段。需要从本文中获取净收入RS 35000作为数字。只有35000,示例文字如bellow,
INCOME GENERATION PER MONTH
------------------------------------------
INCOME FROM RETAIL SALE OF VEGETABLE BUSINESS RS 30,000/-
INCOME FROM WHOLE SALE OF VEGETABLE BUSINESS- RS 40,000/-
TOTAL INCOME APPROX- RS 70,000/-
TOTAL EXPENSES- RS 35,000/-
[SALARIES, FUEL, OTHER EXPENSES]
NET INCOME- RS 35,000/-
答案 0 :(得分:0)
在SQL Server中,当您确定该值具有此模式时,最好选择替换
select cast(replace(replace(replace( 'RS 35,000/','RS',''),',',''),'/','') as
numeric(18,3))
答案 1 :(得分:0)
使用Patindex我们可以提取数值
Declare @String TABLE (INCOME_GENERATION_PER_MONTH nvarchar(max))
INSERT INTO @String
SELECT 'INCOME FROM RETAIL SALE OF VEGETABLE BUSINESS RS 30,000/-' UNION ALL
SELECT 'INCOME FROM WHOLE SALE OF VEGETABLE BUSINESS- RS 40,000/-' UNION ALL
SELECT 'TOTAL INCOME APPROX- RS 70,000/-' UNION ALL
SELECT 'TOTAL EXPENSES- RS 35,000/-' UNION ALL
SELECT '[SALARIES, FUEL, OTHER EXPENSES]' UNION ALL
SELECT 'NET INCOME- RS 35,000/-'
SELECT
'Rs.' + CAST(CONVERT(varchar, CAST(INCOME_GENERATION_PER_MONTH AS Money), 0) AS varchar) + '/-' AS INCOME_GENERATION_PER_MONTH
FROM (SELECT
REPLACE(REPLACE((SUBSTRING(INCOME_GENERATION_PER_MONTH, PATINDEX('%[^[A-Z -]%', INCOME_GENERATION_PER_MONTH), LEN(INCOME_GENERATION_PER_MONTH))), '/-', ''), ',', '') AS INCOME_GENERATION_PER_MONTH,
ISNUMERIC(REPLACE(REPLACE((SUBSTRING(INCOME_GENERATION_PER_MONTH, PATINDEX('%[^[A-Z -]%', INCOME_GENERATION_PER_MONTH), LEN(INCOME_GENERATION_PER_MONTH))), '/-', ''), ',', '')) AS ISnumericval
FROM @String) dt
WHERE dt.ISnumericval = 1
结果
INCOME_GENERATION_PER_MONTH
----------------------------
Rs.30000.00/-
Rs.40000.00/-
Rs.70000.00/-
Rs.35000.00/-
Rs.35000.00/-
答案 2 :(得分:0)
这应该通过一些字符串操作来实现,假设值周围的字符始终存在并且标准的方式是您发布样本数据的方式。如果出于测试目的需要处理任何其他情况,您可以运行以下内容并对其进行修改:
CREATE TABLE #income (clientIncome NVARCHAR(200));
INSERT INTO #income
(
clientIncome
)
VALUES ('INCOME FROM RETAIL SALE OF VEGETABLE BUSINESS RS 30,000/-'),
('INCOME FROM WHOLE SALE OF VEGETABLE BUSINESS- RS 40,000/-'),
('TOTAL INCOME APPROX- RS 70,000/-'),
('TOTAL EXPENSES- RS 35,000/-'),
('[SALARIES, FUEL, OTHER EXPENSES]'),
('NET INCOME- RS 35,000/-');
SELECT i.clientIncome,
CASE
WHEN CHARINDEX('RS ', i.clientIncome) != 0 THEN
CAST(replace(SUBSTRING(
i.clientIncome,
CHARINDEX('RS ', i.clientIncome) + 3,
CHARINDEX('/-', i.clientIncome) - CHARINDEX('RS ', i.clientIncome) - 3
),',','') AS NUMERIC)
ELSE
NULL
END income
FROM #income AS i;
DROP TABLE #income;
<强>产地:强>
clientIncome income
INCOME FROM RETAIL SALE OF VEGETABLE BUSINESS RS 30,000/- 30000
INCOME FROM WHOLE SALE OF VEGETABLE BUSINESS- RS 40,000/- 40000
TOTAL INCOME APPROX- RS 70,000/- 70000
TOTAL EXPENSES- RS 35,000/- 35000
[SALARIES, FUEL, OTHER EXPENSES] NULL
NET INCOME- RS 35,000/- 35000
这将提取值,删除逗号并为您转换为数字。
答案 3 :(得分:0)
您可以创建标量函数:
CREATE FUNCTION [dbo].[ExtractNetAmount]
(
@FieldText varchar(1000)
)
RETURNS varchar(50)
AS
BEGIN
declare @Result decimal(12, 3);
declare @netIncomeKeword varchar(100) = 'NET INCOME- RS '
declare @temp1 varchar(100)
declare @temp2 varchar(100)
set @temp1 = SUBSTRING(@FieldText, PATINDEX('%' + @netIncomeKeword +'[0-9]%', @FieldText) + LEN(@netIncomeKeword), LEN(@FieldText))
set @temp2 = REPLACE(SUBSTRING(@temp1,0, charindex('/', @temp1)),',','')
set @Result = CAST(@temp2 as decimal(12, 3))
RETURN @Result;
END
<强>假设:强>
文字
NET INCOME- RS
金额始终以/
<强>用法:强>
declare @FieldText varchar(1000) = 'INCOME FROM RETAIL SALE OF VEGETABLE
BUSINESS RS 30,000/- INCOME FROM WHOLE SALE OF VEGETABLE BUSINESS- RS 40,000/-
TOTAL INCOME APPROX- RS 70,000/- TOTAL EXPENSES- RS 35,000/-
[SALARIES, FUEL, OTHER EXPENSES] NET INCOME- RS 35,000/-
'
select dbo.[ExtractNetAmount](@fieldtext)
它仅从整个文本中提取Net Income
值,即在给定示例中为35000.000
。
答案 4 :(得分:0)
怎么样:
DECLARE @value NVARCHAR(MAX)
SET @value = N'RS 35,000/ASDF' --anything your value is
WHILE PATINDEX('%[^0-9]%', @value) > 0
SET @value = REPLACE(@value, SUBSTRING(@value, PATINDEX('%[^0-9]%', @value), 1), '' )
SELECT @value