I would like to select the average of a numerical substring within a column defined as varchar. A mass conversion does not work because there is some data in this string that contains non-numeric characters. I would like to ignore these non-numeric strings.

My query looks something like this:

select i.location, avg(convert(int,substring(b.text,8,4)))
from item i, bib b
where i.bib# = b.bib#
and b.tag = '008'
group by i.location

Any thoughts on how to do this?

有帮助吗?

解决方案

Try this:

select i.location, avg(convert(int,substring(b.text,8,4)))
from item i
join bib b on i.bib#=b.bib#
where b.tag = '008'
and isNumeric(substring(b.text,8,4))=1
group by i.location
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top