Average of decimal values : decimal « Data Type « SQL Server / T-SQL






Average of decimal values

 


6> CREATE TABLE T (
7>     int1 int,
8>     bit1 bit,
9>     varchar1 varchar(3),
10>     dec1 dec(5,2),
11>     cmp1 AS (int1 + bit1)
12> )
13> GO
1>
2> INSERT T (int1, bit1) VALUES (1, 0)
3> INSERT T (int1, varchar1) VALUES (2, 'abc')
4> INSERT T (int1, dec1) VALUES (3, 5.25)
5> INSERT T (bit1, dec1) VALUES (1, 9.75)
6> GO

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)
1> SELECT CAST(AVG(dec1) AS dec(5,2)) 'Avg of dec1'
2> FROM T
3> WHERE dec1 IS NOT NULL
4> GO
Avg of dec1
-----------
       7.50

(1 rows affected)
1> drop table t;
2> GO
1>
2>

 








Related examples in the same category

1.decimal(15,4)
2.Use select to do the calculation
3.Function returning decimal
4.Cast int to decimal
5.Cast real number to decimal
6.Cast decimal to varchar and append another string