从T-SQL中的字符串中提取最大数字

时间:2015-04-15 14:38:59

标签: sql sql-server tsql string-matching extraction

我正在导入处理从excel文件导入的数据。有一个字符串,可以包含多个数字。我试图提取字符串中的最大数字或如果没有字符串则提取0。 字符串的格式类似于: " 100%消费后再生纸,50%消费后再生纸,90%消费后再生纸。" "论文包含30%的消费后内容。" 或者有时是一个空字符串或null。

鉴于字符串的不规则格式,我遇到了麻烦,任何帮助都会受到赞赏。

4 个答案:

答案 0 :(得分:1)

  1. 将数据按原样拉入SQL
  2. 编写查询以获取该列中不同的选项列表
  3. 添加新列以存储所需的值
  4. 编写更新语句以填充新列
  5. 就确定最大尺寸而言,我认为您需要首先查看您的数据集,但更新可能非常简单:

    DECLARE @COUNTER INT=1000
    While EXISTS (SELECT * FROM <Table> WHERE NewColumn is NULL) AND @COUNTER>=0
    BEGIN
       UPDATE <Table> SET NewColumn=@COUNTER WHERE <SearchColumn> LIKE '%' + CONVERT(VARCHAR,@COUNTER) + '%' AND NewColumn is NULL
    
       SET @COUNTER=@COUNTER-1
    END
    

答案 1 :(得分:0)

这是一个标量函数,它将一个字符串作为输入并返回它找到的最大整数(最多3位数,但是根据你的问题,我假设你正在处理百分比。如果你需要更多数字,无限重复IF语句。

将其粘贴到SSMS中并运行它以创建该功能。要打电话,请执行以下操作:

SELECT dbo.GetLargestNumberFromString(MyStringField) as [Largest Number in String]
FROM MyMessedUpData

功能:

CREATE FUNCTION GetLargestNumberFromString
(
@s varchar(max)
) 
RETURNS int
AS
BEGIN

    DECLARE @LargestNumber int, @i int
    SET @i = 1
    SET @LargestNumber = 0

    WHILE @i <= LEN(@s)
    BEGIN

    IF SUBSTRING(@s, @i, 3) like '[0-9][0-9][0-9]'
        BEGIN
            IF  CAST(SUBSTRING(@s, @i,3) as int) > @LargestNumber OR @LargestNumber IS NULL
            SET @LargestNumber = CAST(SUBSTRING(@s, @i,3) as int);
        END

    IF SUBSTRING(@s, @i, 2) like '[0-9][0-9]'
        BEGIN
            IF  CAST(SUBSTRING(@s, @i,2) as int) > @LargestNumber OR @LargestNumber IS NULL
            SET @LargestNumber = CAST(SUBSTRING(@s, @i,2) as int);
        END

    IF SUBSTRING(@s, @i, 1) like '[0-9]' OR @LargestNumber IS NULL
        BEGIN
            IF  CAST(SUBSTRING(@s, @i,1) as int) > @LargestNumber
            SET @LargestNumber = CAST(SUBSTRING(@s, @i,1) as int);
        END

    SET @i = @i + 1


    CONTINUE
    END
    RETURN @LargestNumber
END

答案 2 :(得分:0)

<强> SQL Fiddle Demo

生成LEN(txt) RIGHT()txt片段int。在第一个非数字字符处修剪每个片段。测试余数是否为MAX()。返回SELECT txt ,MAX(TRY_CONVERT(int,LEFT(RIGHT(txt,i),PATINDEX('%[^0-9]%',RIGHT(txt,i)+' ')-1))) FROM MyTable CROSS APPLY ( SELECT TOP(LEN(txt)) ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) i FROM master.dbo.spt_values a, master.dbo.spt_values b ) x GROUP BY txt

{{1}}

答案 3 :(得分:0)

我最终创建了一个处理它的函数。这是代码:

CREATE FUNCTION [dbo].[cal_GetMaxPercentFromString] 

RETURNS float
AS
BEGIN
    declare @Numbers Table(number float)
    insert into @Numbers
    Select 0

    declare @temp as varchar(2000) = @string
    declare @position int, @length int, @offset int

    WHILE CHARINDEX('%', @temp) > 0
    BEGIN
        set @position = CHARINDEX('%', @temp)
        set @offset = 1
        set @length = -1
        WHILE @position - @offset > 0 and @length < 0
        BEGIN
            if SUBSTRING(@temp, @position - @offset, 1) not LIKE '[0-9]'
                set @length = @offset - 1    
            set @offset = @offset + 1
        END

        if @length > 0
        BEGIN
            insert into @Numbers
            select CAST(SUBSTRING(@temp, @position - @length, @length) as float)            
        END
        set @temp = SUBSTRING(@temp, 1, @position - 1) + SUBSTRING(@temp, @position + 1, LEN(@temp) - @position)
    END

    declare @return as float
    select @return = MAX(number) from @Numbers
    return @return
END