Jeff Chambers
Jeff Chambers

Reputation: 11

select, avg statement not working

I have a table called Employee with about 17 different columns on it. One of those columns is called age. I am suppose to write up a statement that will get the average age of all the employees with an AS in the statement. Here is the statement I have written up and unfortunately I continue to get an error message. Also, I am new at this so please don't beat me to hard...lol.

SELECT AVG(Age) AS Average_Age FROM Employee

I get below error..

msg 8117, state 16, line 1 invalid syntax for operand avg.

I believe the message is telling me that the avg is trying to calculate, but cannot do it.

Why did I see this done on a youtube video and it work perfectly for the person who created it? While I did the same exact statement I got it wrong...can someone explain that?

Update as Per comments:

Error Message:

Operand data type varchar is invalid for avg operator.

Upvotes: 0

Views: 742

Answers (1)

Ian Kenney
Ian Kenney

Reputation: 6426

If age is a varchar column you can try

SELECT AVG(cast(Age as int)) AS Average_Age FROM Employee

( but you may want to think about changing the datatype )

Upvotes: 1

Related Questions