Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
sql_server 217
t-sql 211
tsql 116
sqlserver 96
BRH 78
#SQLServer 66
#TSQL 56
SQL Server 34
function 11
SSMS 9

Archive · View All
August 2007 17
August 2010 8
June 2012 7
June 2011 7
November 2007 7
August 2012 6
May 2012 6
November 2011 6
August 2011 6
October 2011 6

Madhivanan's TSQL Blog

Use system functions cleverly

May 17 2012 12:00AM by Madhivanan   

My Co-worker complained me that AVG function is not properly working in SQL Server whereas it works correctly in mysql. I immediately told him that AVG does the implicit convertion by default and the result may be wrong (based on datatype).

Consider the following set of data

select avg(col) from
(
	select 1 as col union all
	select 2
) as t
The result is
average
-----------
1
As you see, the result is not 1.5 but it is 1 becuase the return type of AVG function is same as that of column it is applied for. The datatype of col is INT and AVG function CASTs 1.5 into 1. To avoid this you need to CAST the datatype into DECIMAL (or multiply column by 1.0)
select avg(col*1.0) as average,avg(cast(col as decimal(12,2))) as average from
(
	select 1 as col union all
	select 2
) as t
The result is
average                                 average
--------------------------------------- ---------------------------------------
1.500000                                1.500000
So you need to aware of this when using system functions whose datatype depends on the datatype of the column

Tags: 


Madhivanan
3 · 39% · 12441
19
 
0
Lifesaver
 
 
 
0
Incorrect



Submit

2  Comments  

  • Nice post with nice example Madhivanan. Thanks for sharing.

    commented on May 17 2012 12:11AM
    Hardik Doshi
    20 · 9% · 2839
  • Nice, concise post. Thank-you, Madhivanan!

    commented on May 17 2012 11:17AM
    Nakul Vachhrajani
    4 · 33% · 10587

Your Comment


Sign Up or Login to post a comment.

"Use system functions cleverly" rated 5 out of 5 by 19 readers
Use system functions cleverly , 5.0 out of 5 based on 19 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]