Q:

how to cast a nvarchar to decimal and get average

I have a database table which contains 8 columns. First column is the customer name, and the others are scores but they are stored in nvarchar columns(unfortunately). the reason to store decimals in nvarchar columns is that if there is no score for a customer, they put comma(','). This case is only for column G. That comma gives me a lot of trouble, because I am trying to do the select below 
SELECT
GKOD ,
AVG(CAST(CSI as decimal)),
AVG(CAST(A as decimal)),
AVG(CAST(B as decimal)),
AVG(CAST(C as decimal)),
AVG(CAST(D as decimal)),
AVG(CAST(E as decimal)),
AVG(CAST(F as decimal)),
AVG(
    CASE WHEN ISNUMERIC(G) >= 0
    THEN CAST(G AS decimal)
    ELSE 0 END
)
FROM ANALIZ_DATA WHERE (YIL*100+CAST(AY AS INT)) between 201401  AND 201405
GROUP BY GKOD 

For column G, I am trying to check if it is numeric. but it gives this error
Error converting data type nvarchar to numeric.

What am I doing wrong?

A:

isnumeric returns 0 or 1, so your condition is not doing anything.  Instead, compare just to 1:
AVG(CASE WHEN ISNUMERIC(G) = 1
    THEN CAST(G AS decimal)
    ELSE 0 END
)

Also, are you sure you want to use the value 0 for else?  This treats non-numeric values as 0 instead of ignoring them.  You might want to treat them only as NULL:
AVG(CASE WHEN ISNUMERIC(G) = 1
    THEN CAST(G AS decimal)
    END
)

I would also be inclined to include the precision and scale arguments, instead of relying on defaults.

