sum求值

yubofighting 2012-03-17 09:09:22
table A中有很多字段,比如a1、a2、a3等

三个字段中有字母和数字,比如:B3、C1、D\4等

现在要求各字段的数字和(排除字母和字符),我现在想到的一个方法是
把replace函数写成自定义的一个函数在函数里面去逐个替换(可以当字符是固定的几个),如:
sum(replace(replace(replace(replace(a1,'B',''),'C',''),'D',''),'\',''))


不知道还有没有更好的办法?
...全文
114 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复

go
create table #s(
col varchar(10)
)
insert #s
select 'B3' union all
select 'C1' union all
select 'D\4' union all
select '4MD' union all
select 'AR/12BD'
;with t
as(
select substring(col+' ',patindex( '%[0-9]% ',col+' '),len(col+' ')) as col1
from #s
)
select left(col1+' ',patindex( '%[^0-9]% ',col1+' ')-1) as col from t
/*
col
3
1
4
12
4
*/
--加了一种情况,也可以的
dawugui 2012-03-17
  • 打赏
  • 举报
回复
我用函数帮你实现,看是否合适?
create table A(a1 varchar(10),a2 varchar(10),a3 varchar(10))
insert into a values('B3','C1','D\4')
insert into a values('B31d','C12','D1\4')
insert into a values('B31ds2','C13','D2\4')
go

create function dbo.f_str(@a varchar(10)) returns int
as
begin
declare @cnt as int
set @cnt = 0
declare @i as int
declare @j as int
declare @k1 as int
declare @k2 as int
set @i = 1
set @j = len(@a)
set @k1 = 0
set @k2 = 0
while @i <= @j
begin
if substring(@a , @i , 1) between '0' and '9'
begin
if @k1 = 0
set @k1 = @i
if @i = @j
begin
set @k2 = @j
set @cnt = @cnt + cast(substring(@a , @k1 , (@k2 - @k1 + 1)) as int)
end
end
else
begin
if @k1 > 0
begin
set @k2 = @i - 1
set @cnt = @cnt + cast(substring(@a , @k1 , (@k2 - @k1 + 1)) as int)
end
set @k1 = 0
set @k2 = 0
end
set @i = @i + 1
end

return @cnt
end
go

--调用函数
select * , [sum] = dbo.f_str(a1) + dbo.f_str(a2) + dbo.f_str(a3) from a

drop function dbo.f_str

drop table a

/*
a1 a2 a3 sum
---------- ---------- ---------- -----------
B3 C1 D\4 8
B31d C12 D1\4 48
B31ds2 C13 D2\4 52

(所影响的行数为 3 行)
*/
  • 打赏
  • 举报
回复

go
create table #s(
col varchar(10)
)
insert #s
select 'B3' union all
select 'C1' union all
select 'D\4' union all
select 'AR/12BD'
;with t
as(
select substring(col+' ',patindex( '%[0-9]% ',col+' '),len(col+' ')) as col1
from #s
)
select left(col1+' ',patindex( '%[^0-9]% ',col1+' ')-1) as col from t
/*
col
3
1
4
12
*/

--必须在字符串最右边加一个‘空格’才可以使用
yubofighting 2012-03-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 dawugui 的回复:]
不知道你是否还存在这样的数据?

ads2324dsaf
[/Quote]
有可能存在,有没有单独拿出数字的好方法?

但是实在复杂,按照业务需求,数字只会出现在最前面或者最后面,我修改了上面的代码
declare @str varchar(100),@str2 varchar(100)
set @str= '\xvnvnbvn33'
set @str2=''
select @str2=
case when patindex( '%[0-9]%',@str)<patindex( '%[^0-9]%',@str) then
substring(@str,patindex( '%[0-9]%',@str),patindex( '%[^0-9]%',@str)-1) --得到以第一个数字开始的字符串
else substring(@str,patindex( '%[0-9]%',@str),LEN(@str)) end
--select @str2=left(@str2,patindex( '%[^0-9]% ',@str2)-1) --得到第一个非数字开始的位置,并根据此位置删除其及以后的字符

print @str2 --显示结果
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yubofighting 的回复:]

引用 3 楼 travylee 的回复:
SQL code


如果无规律,就用这个方法就可以了

declare @str varchar(100),@str2 varchar(100)
set @str= 'asdl;fkjqwopi-437y69t8ewasdlmbh495u95tofdms74547t '

select @str2=substrin……
[/Quote]

为什么不这样col+'某个字符'??
dawugui 2012-03-17
  • 打赏
  • 举报
回复
不知道你是否还存在这样的数据?

ads2324dsaf
yubofighting 2012-03-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 travylee 的回复:]
SQL code


如果无规律,就用这个方法就可以了

declare @str varchar(100),@str2 varchar(100)
set @str= 'asdl;fkjqwopi-437y69t8ewasdlmbh495u95tofdms74547t '

select @str2=substring(@str,patindex( '%[……
[/Quote]

这个数字在末尾的时候会有问题
  • 打赏
  • 举报
回复


如果无规律,就用这个方法就可以了

declare @str varchar(100),@str2 varchar(100)
set @str= 'asdl;fkjqwopi-437y69t8ewasdlmbh495u95tofdms74547t '

select @str2=substring(@str,patindex( '%[0-9]% ',@str),len(@str)) --得到以第一个数字开始的字符串
,@str2=left(@str2,patindex( '%[^0-9]% ',@str2)-1) --得到第一个非数字开始的位置,并根据此位置删除其及以后的字符

print @str2 --显示结果
yubofighting 2012-03-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 travylee 的回复:]
引用楼主 yubofighting 的回复:
table A中有很多字段,比如a1、a2、a3等

三个字段中有字母和数字,比如:B3、C1、D\4等

现在要求各字段的数字和(排除字母和字符),我现在想到的一个方法是
把replace函数写成自定义的一个函数在函数里面去逐个替换(可以当字符是固定的几个),如:
sum(replace(replace(replace(replace(……
[/Quote]
数字不一定在右边,不一定是0-9,不然直接substring截取就可以了
  • 打赏
  • 举报
回复
[Quote=引用楼主 yubofighting 的回复:]
table A中有很多字段,比如a1、a2、a3等

三个字段中有字母和数字,比如:B3、C1、D\4等

现在要求各字段的数字和(排除字母和字符),我现在想到的一个方法是
把replace函数写成自定义的一个函数在函数里面去逐个替换(可以当字符是固定的几个),如:
sum(replace(replace(replace(replace(a1,'B',''),'C',''),'D','……
[/Quote]

你的数字都是在右边么?或者数字都是0-9还是怎么的

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧