如:'库房1号' , '11号监测点' ,字符串格式并不固定,数字位置可能不同,数字大小长度可能不同,有的甚至不含数字,怎么取出字符串最后一个包含数字的值呢.

解决方案 »

  1.   

    select substring(reverse('库房1号'),PATINDEX('%[0-9]%',reverse('库房1号')),1)
      

  2.   

    --> 测试数据: #tb
    if object_id('tempdb.dbo.#tb') is not null drop table #tb
    go
    create table #tb (id varchar(10))
    insert into #tb
    select '库房1号' union all
    select '11号监测点'
    if object_id('f_str')is not null drop function f_str
    go 
    create function f_str(@id varchar(100))
    returns varchar(10)
    as 
     begin 
      declare @str varchar(100),@lenNUM int ,@len int
      set @str=''
      set @lenNUM=patindex('%[0-9]%',reverse(@id))   --倒转后第一个数字起始位
      set @len=len(@id)  while @lenNum<=@len
       begin 
         if isnumeric(substring(REVERSE(@id),@lenNUM,1))=1
     set @str=@str+substring(REVERSE(@id),@lenNUM,1)
         
         set @lenNUM=@lenNUM+1
       end
      return @str
     end
    go select id,number=dbo.f_str(id) from #tb  
    id         number
    ---------- ----------
    库房1号       1
    11号监测点     11(2 行受影响)
      

  3.   

    有个问题,楼主这字符串中会不会有多个数值的情况??????DECLARE @str VARCHAR(20)
    SET @str = '11号监测点'SELECT SUBSTRING(@str,PATINDEX('%[0-9]%',@str),LEN(@str) - PATINDEX('%[0-9]%',REVERSE(@str)) - PATINDEX('%0-9%',@str) - PATINDEX('%[0-9]%',@str) + 2)SET @str = '库房1号'SELECT SUBSTRING(@str,PATINDEX('%[0-9]%',@str),LEN(@str) - PATINDEX('%[0-9]%',REVERSE(@str)) - PATINDEX('%0-9%',@str) - PATINDEX('%[0-9]%',@str) + 2)
    /*
    -------------------- 
    11(所影响的行数为 1 行)                     
    -------------------- 
    1(所影响的行数为 1 行)
    */
      

  4.   

    DECLARE @STR VARCHAR(50)
    SELECT @STR='A1234号监测点'
    SELECT REVERSE(STUFF(REVERSE(STUFF(@STR,1,PATINDEX('%[0-9]%',@STR)-1,'')),1,PATINDEX('%[0-9]%',REVERSE(STUFF(@STR,1,PATINDEX('%[0-9]%',@STR)-1,'')))-1,''))
    --1234
      

  5.   

    加入对于小数点的支持,并且去掉结尾的.和.后的0
    DECLARE @STR VARCHAR(50)
    SELECT @STR='A1234.560号监测点'
    SELECT CONVERT(FLOAT,REVERSE(STUFF(REVERSE(STUFF(@STR,1,PATINDEX('%[0-9.]%',@STR)-1,'')),1,PATINDEX('%[0-9.]%',REVERSE(STUFF(@STR,1,PATINDEX('%[0-9.]%',@STR)-1,'')))-1,'')))
    --1234.56
      

  6.   

    LS的有错,有这个
    DECLARE @STR VARCHAR(50)
    SELECT @STR='ae12rd1234.560号监测点'
    SELECT 
    CASE WHEN STUFF(REVERSE(@STR),1,PATINDEX('%[0-9.]%',REVERSE(@STR))-1,'') LIKE '%[^0-9.]%' THEN
    CONVERT(FLOAT,REVERSE(STUFF(STUFF(REVERSE(@STR),1,PATINDEX('%[0-9.]%',REVERSE(@STR))-1,''),PATINDEX('%[^0-9.]%',STUFF(REVERSE(@STR),1,PATINDEX('%[0-9.]%',REVERSE(@STR))-1,'')),LEN(STUFF(REVERSE(@STR),1,PATINDEX('%[0-9.]%',REVERSE(@STR))-1,'')),'')))
    ELSE 
    CONVERT(FLOAT,STUFF(REVERSE(@STR),1,PATINDEX('%[0-9.]%',REVERSE(@STR))-1,''))
    END
    --1234.56
      

  7.   

    --> 测试数据: #tb
    if object_id('tempdb.dbo.#tb') is not null drop table #tb
    go
    create table #tb (id varchar(10))
    insert into #tb
    select '库房1号' union all
    select '11号监测点'
    select id,
    reverse(substring(reverse(left(id,len(id)-patindex('%[0-9]%',reverse(id))+1)),
      1,
      patindex('%[^0-9]%',
       reverse(left(id,len(id)-patindex('%[0-9]%',reverse(id))+1))+'|'
      )-1)
    )
    from #tb id         
    ---------- ----------
    库房1号       1
    11号监测点     11(2 行受影响)