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
Question
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?
Solution
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