用自定义函数剃除字段中符串中大于9位以上的数字

问题贴:http://topic.csdn.net/u/20100329/19/222fc2d6-e65f-40c1-af8a-ca577a7a510e.html?seed=1857721250

 

--把字段中连续9位的数字都删除掉

--------------------------------------------------------------------------

--  Author : htl258(Tony)

--  Date   : 2010-03-29 19:41:17

--  Version:Microsoft SQL Server 2008 (SP1) - 10.0.2531.0 (Intel X86)

--          Mar 29 2009 10:27:29

--          Copyright (c) 1988-2008 Microsoft Corporation

--          Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 2)

--------------------------------------------------------------------------

--> 生成测试数据表:tb

 

IF NOT OBJECT_ID('[tb]') IS NULL

    DROP TABLE [tb]

GO

CREATE TABLE [tb]([a] NVARCHAR(100))

INSERT [tb]

SELECT 'Mar13845673456CopyrightQQ:123456789Corporation' UNION ALL

SELECT 'abc258521123226MicrosoftQQ:2541254876587' UNION ALL

SELECT 'acd2585WindowsQQ:2541254876587' UNION ALL

SELECT '258ded2585EditionQQ:2541'

GO

--SELECT * FROM [tb]

 

-->SQL查询如下:

if OBJECT_ID('fn_tony') is not null

    drop function fn_tony

go

create function fn_tony(

@s nvarchar(4000)

)returns nvarchar(4000)

as

begin

    declare @r nvarchar(4000)

    set @r=''

    while len(@s)>0 

    begin

       if len(left(@s,patindex('%[^0-9]%',@s+'a')-1))<9

       set @r=@r+left(@s,patindex('%[^0-9]%',@s+'a')-1)                     

       set @s=stuff(@s,1,patindex('%[^0-9]%',@s+'a')-1,'') 

       set @r=@r+left(@s,patindex('%[0-9]%',@s+'0')-1)

       set @s=stuff(@s,1,patindex('%[0-9]%',@s+'0')-1,'')

    end   

    return @r

end

go

 

update tb set a=dbo.fn_tony(a)

 

select * from tb

/*

a

---------------------------------

MarCopyrightQQ:Corporation

abcMicrosoftQQ:

acd2585WindowsQQ:

258ded2585EditionQQ:2541

 

(4 行受影响)

*/

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值