문제

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