Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 AVG return decimal

Author  Topic 

ckuo@kahluadesigns.com
Yak Posting Veteran

58 Posts

Posted - 2002-11-08 : 14:47:44

Why is this statement not returning 4.5? How can I get a decimal value?

SELECT AVG(Rating) FROM Table

TABLE
Rating
5
4

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2002-11-08 : 15:21:41
Your datatype for rating is probably integer. Integer represents whole numbers only. You need to cast/convert the rating column to float or whatever. Here is a sample:

CREATE TABLE asdf( col1 int not null)

INSERT INTO asdf VALUES(4)
INSERT INTO asdf VALUES(5)

SELECT col1 FROM ASDF

SELECT AVG(CAST(col1 AS float)) FROM asdf

DROP TABLE ASDF




Edited by - tduggan on 11/08/2002 15:27:11
Go to Top of Page

burbakei
Yak Posting Veteran

80 Posts

Posted - 2002-11-13 : 13:57:01
Another way is
SELECT AVG(Rating * 1.) FROM Table



Go to Top of Page
   

- Advertisement -