我有这样一些数据!
3弄全
4弄雙102號以上
6弄全
2弄全
90弄全
單53弄以下
4弄單35之1號以上
176弄全
8弄全
連256弄至399之13號
4弄單35號以下
1弄全
4弄雙100號以下
1弄全
單3弄以下
其中弄之前还可能会存在除了多少弄之外的别的带有数字的数据。如:154巷54弄單11號以下
我现在想知道该怎么找到"弄"之前的最近一个数字并返回它。

解决方案 »

  1.   


    --> 测试数据: @s
    declare @s table (val varchar(50))
    insert into @s
    select '4弄雙102號以上' union all
    select '6弄全' union all
    select '2弄全' union all
    select '90弄全' union all
    select '單53弄以下' union all
    select '4弄單35之1號以上' union all
    select '176弄全' union all
    select '8弄全' union all
    select '連256弄至399之13號' union all
    select '4弄單35號以下' union all
    select '1弄全' union all
    select '4弄雙100號以下' union all
    select '1弄全' union all
    select '單3弄以下' union all
    select '154巷54弄單11號以下'select case patindex('%[^0-9]%',reverse(left(val,charindex('弄',val)-1)))
    when 0 then left(val,charindex('弄',val)-1)
    else reverse(left(reverse(left(val,charindex('弄',val)-1)),patindex('%[^0-9]%',reverse(left(val,charindex('弄',val)-1)))-1)) end
    from @s
      

  2.   

    出错的我不太清楚是哪条数据,是substring的length无效!
      

  3.   

    WHEN More like '%弄%'
    THEN dbo.Lgtc_Fn_ToNum(substring(More,charindex('弄',More)-2,charindex('弄',More,charindex('弄至',More))))
    这句还能查到三十五行的~
    你那句在第一行就出错了。
      

  4.   

    把二楼的改一下:select 
        case when charindex('弄',val)>0 then
            case patindex('%[^0-9]%',reverse(left(val,charindex('弄',val)-1)))
            when 0 then left(val,charindex('弄',val)-1)
            else reverse(left(reverse(left(val,charindex('弄',val)-1)),patindex('%[^0-9]%',reverse(left(val,charindex('弄',val)-1)))-1)) 
            end
        else
            ''
        end
    from @s
      

  5.   


    like 相当于charindex('弄',val)>0了,再有问题的话,
    看你的
    函数Lgtc_Fn_ToNum里可能也有类似问题
      

  6.   


    --这应该比较全了吧?
    --> 测试数据: @s
    declare @s table (val varchar(18))
    insert into @s
    select '雙20號至148號' union all
    select '雙48號以上' union all
    select '23號' union all
    select '單63之1號至127號' union all
    select '11之1號' union all
    select '154巷48弄單9號以上' union all
    select '300巷17弄全' union all
    select '26之10至之11號' union all
    select '連256弄至399之13號' union all
    select '全'  union all
    select '弄'select case when charindex('弄',val)>0 then case patindex('%[^0-9]%',reverse(left(val,charindex('弄',val)-1)))
    when 0 then left(val,charindex('弄',val)-1)
    else reverse(left(reverse(left(val,charindex('弄',val)-1)),patindex('%[^0-9]%',reverse(left(val,charindex('弄',val)-1)))-1)) end
    else '' end
    from @s
      

  7.   

    TO 8楼。那句确实能转三十五行。也是在substring那里报错。可能哪里没考虑好。而且只能取到弄前的两个数字!~改成
    substring(More,charindex('弄',More)-3,charindex('弄',More))又取不到数字。substring(More,charindex('弄',More)-1,charindex('弄',More))又只能取一个数字。
    背着灵魂!你的语句在所有数据中测试没有问题了。
    不过我就是看不懂~
      

  8.   


    这个哪里看不懂,其实道理很简单。
    第一步:先判断数据中是否存在“弄”,如果不存在,直接返回"",
    第二步:如果存在“弄”,截取到“弄”之前的字符串,如原数据是“154巷48aaa弄單9號以上”,截取后为“154巷48aaa”
    第三步:再将截取后的字符反转,并返回第一次出现的数字。