要求去掉该字段里连续5位以上数字(包含5位)
连续5位以下不过滤
程序里面写可能比较简单
但用sql语句不知如何实现
所以想请教下各位
多谢了
8 个解决方案
#1
declare @model varchar(20)
set @model='sdfa102345sdfsd'
if patindex('%[0-9][0-9][0-9][0-9][0-9]%', @model)>0
print 'found'
#2
delete tb where patindex('%[0-9][0-9][0-9][0-9][0-9]%', model)>0
#3
过滤容易,替换还是要自己写循环。
#4
create table tb
(
myValue nvarchar(50)
)
insert tb
select 'afsdsa12345fsdf12345' union all
select 'afsdiia123456fsdf'
select
patindex('%[0-9][0-9][0-9][0-9][0-9]%', myValue)
from tb
drop table tb
结果:
7
8
----------------------------------------
像楼上,判断容易,替换的话还是要写函数循环判断
#5
CREATE TABLE test72(model varchar(30))
INSERT test72 SELECT '2asdladfas03874923874a;asdj'
UNION ALL SELECT '2as3addl20382asdfasd'
UNION ALL SELECT '2asdlg703874923'
UNION ALL SELECT '342342342asdl203874923'
go
CREATE FUNCTION getTest72(@MODEL varchar(100))
RETURNS varchar(100)
AS
BEGIN
DECLARE @p int
DECLARE @R VARCHAR(100),@H varchar(100)
SET @p=PATINDEX('%[0-9][0-9][0-9][0-9][0-9]%',@model)
IF @P=0
SET @R=@MODEL
ELSE
BEGIN
SET @H=left(@MODEL,@p-1)
SET @H=@H+stuff(STUFF(@MODEL,1,@p-1,''),1,PATINDEX('%[^0-9]%',STUFF(@MODEL,1,@p-1,'')+' ')-1,'')
SET @R=dbo.getTest72(@H)
END
RETURN @r
END
go
SELECT dbo.gettest72(model) FROM test72 t
go
--result
/*
----------------------------------
2asdladfasa;asdj
2as3addlasdfasd
2asdlg
asdl
(所影响的行数为 4 行)*/
#6
--> 测试数据: [s]
if object_id('[s]') is not null drop table [s]
create table [s] (num varchar(14))
insert into [s]
select '123312asdf3f43' union all
select '1232asdf3f43' union all
select 'sdfawef43f345' union all
select 'sdfasdfsaf' union all
select '3425234' union all
select 'sdf4g4544'
go
create function fs(@num varchar(100))
returns varchar(100)
as
begin
declare @star int,@num_new varchar(100),@new varchar(100)
while(patindex('%[0-9]%',@num)>0)
begin
set @star=patindex('%[0-9]%',@num)
set @num_new=isnull(@num_new,'') + substring(@num,1,@star-1)
set @new=substring(@num,@star,len(@num))
if(patindex('%[^0-9]%',@new)>6)
begin
set @num_new = @num_new + ''
set @num = substring(@new,patindex('%[^0-9]%',@new),len(@new))
end
else if(patindex('%[^0-9]%',@new)=0)
begin
set @num_new = @num_new + case when len(@new)>=5 then '' else @new end
set @num=''
end
else
begin
set @num_new = @num_new + substring(@new,1,patindex('%[^0-9]%',@new)-1)
set @num = substring(@new,patindex('%[^0-9]%',@new),len(@new))
end
end
set @num_new=isnull(@num_new,'')+@num
return @num_new
end
go
select num=dbo.fs(num) from [s]
go
--结果:
num
------------
asdf3f43
1232asdf3f43
sdfawef43f345
sdfasdfsaf
sdf4g4544
#7
我说的过滤就是
只把5位含5位以上的数字
该字段其他值不动
#8
IF OBJECT_ID('DBO.GET_NUMBER2') IS NOT NULL
DROP FUNCTION DBO.GET_NUMBER2
GO
CREATE FUNCTION DBO.GET_NUMBER2(@S VARCHAR(100))
RETURNS VARCHAR(100)
AS
BEGIN
WHILE PATINDEX('%[^0-9]%',@S) > 0
BEGIN
set @s=stuff(@s,patindex('%[^0-9]%',@s),1,'')
END
RETURN @S
END
GO
--测试
PRINT DBO.GET_NUMBER2('呵呵ABC123ABC')
#1
declare @model varchar(20)
set @model='sdfa102345sdfsd'
if patindex('%[0-9][0-9][0-9][0-9][0-9]%', @model)>0
print 'found'
#2
delete tb where patindex('%[0-9][0-9][0-9][0-9][0-9]%', model)>0
#3
过滤容易,替换还是要自己写循环。
#4
create table tb
(
myValue nvarchar(50)
)
insert tb
select 'afsdsa12345fsdf12345' union all
select 'afsdiia123456fsdf'
select
patindex('%[0-9][0-9][0-9][0-9][0-9]%', myValue)
from tb
drop table tb
结果:
7
8
----------------------------------------
像楼上,判断容易,替换的话还是要写函数循环判断
#5
CREATE TABLE test72(model varchar(30))
INSERT test72 SELECT '2asdladfas03874923874a;asdj'
UNION ALL SELECT '2as3addl20382asdfasd'
UNION ALL SELECT '2asdlg703874923'
UNION ALL SELECT '342342342asdl203874923'
go
CREATE FUNCTION getTest72(@MODEL varchar(100))
RETURNS varchar(100)
AS
BEGIN
DECLARE @p int
DECLARE @R VARCHAR(100),@H varchar(100)
SET @p=PATINDEX('%[0-9][0-9][0-9][0-9][0-9]%',@model)
IF @P=0
SET @R=@MODEL
ELSE
BEGIN
SET @H=left(@MODEL,@p-1)
SET @H=@H+stuff(STUFF(@MODEL,1,@p-1,''),1,PATINDEX('%[^0-9]%',STUFF(@MODEL,1,@p-1,'')+' ')-1,'')
SET @R=dbo.getTest72(@H)
END
RETURN @r
END
go
SELECT dbo.gettest72(model) FROM test72 t
go
--result
/*
----------------------------------
2asdladfasa;asdj
2as3addlasdfasd
2asdlg
asdl
(所影响的行数为 4 行)*/
#6
--> 测试数据: [s]
if object_id('[s]') is not null drop table [s]
create table [s] (num varchar(14))
insert into [s]
select '123312asdf3f43' union all
select '1232asdf3f43' union all
select 'sdfawef43f345' union all
select 'sdfasdfsaf' union all
select '3425234' union all
select 'sdf4g4544'
go
create function fs(@num varchar(100))
returns varchar(100)
as
begin
declare @star int,@num_new varchar(100),@new varchar(100)
while(patindex('%[0-9]%',@num)>0)
begin
set @star=patindex('%[0-9]%',@num)
set @num_new=isnull(@num_new,'') + substring(@num,1,@star-1)
set @new=substring(@num,@star,len(@num))
if(patindex('%[^0-9]%',@new)>6)
begin
set @num_new = @num_new + ''
set @num = substring(@new,patindex('%[^0-9]%',@new),len(@new))
end
else if(patindex('%[^0-9]%',@new)=0)
begin
set @num_new = @num_new + case when len(@new)>=5 then '' else @new end
set @num=''
end
else
begin
set @num_new = @num_new + substring(@new,1,patindex('%[^0-9]%',@new)-1)
set @num = substring(@new,patindex('%[^0-9]%',@new),len(@new))
end
end
set @num_new=isnull(@num_new,'')+@num
return @num_new
end
go
select num=dbo.fs(num) from [s]
go
--结果:
num
------------
asdf3f43
1232asdf3f43
sdfawef43f345
sdfasdfsaf
sdf4g4544
#7
我说的过滤就是
只把5位含5位以上的数字
该字段其他值不动
#8
IF OBJECT_ID('DBO.GET_NUMBER2') IS NOT NULL
DROP FUNCTION DBO.GET_NUMBER2
GO
CREATE FUNCTION DBO.GET_NUMBER2(@S VARCHAR(100))
RETURNS VARCHAR(100)
AS
BEGIN
WHILE PATINDEX('%[^0-9]%',@S) > 0
BEGIN
set @s=stuff(@s,patindex('%[^0-9]%',@s),1,'')
END
RETURN @S
END
GO
--测试
PRINT DBO.GET_NUMBER2('呵呵ABC123ABC')