日期:2014-05-18  浏览次数:20465 次

sum函数
表中的某一列有数字,有汉字(nvarchar)。如何过滤出数字记录并计算它们的和。

------解决方案--------------------
怎么过滤?
------解决方案--------------------
SQL code

if object_id('tb') is not null
   drop table tb
go
create table tb
(
 id int identity(1,1),
 col1 varchar(10)
)
go
insert into tb (col1)
select '1' union all
select '2' union all
select '你好' union all
select '3'
go
select sum(case when isnumeric(col1)=1 then col1 else 0 end) from tb
go
/*
-----------
6

(1 行受影响)
*/

------解决方案--------------------
是楼上的意思吗?
------解决方案--------------------
select sum(字段) from tb where partindex('%[^0-9]%',字段)=0