Showing posts with label evaluate. Show all posts
Showing posts with label evaluate. Show all posts

Friday, February 24, 2012

Avoiding Divide by Zero in Report

What is the experession to evaluate if the result of a computation would be a divide by zero error for a text box in report?

IIF(divide by zero, display nothing, else display computed result)...?

you should actually use it in your query,but...

=iif(value1 = 0 or value2=0,0, value1/value2)

where the values are the values your dividing by.

it's much easier to do it in sql though...

|||I'd recommend either doing it in your Stored Procedure or creating a function to handle it. You don't want to be putting that expression in every sell in which you're doing division.

You can create a function like the one below and place it in the Report code block or put it in an assembly and reference the assembly in the report.

Public Function CalcAvg(dblNum As Double, dblDen As Double) as Object

if (dblDen = nothing) or (dblDen = 0)
CalcAvg = 0
else
CalcAvg = dblNum / dblDen
end if

End Function|||

I have this code in my Code section

Code Snippet

Public Shared Function DivideXbyY(ByVal X As Decimal, ByVal Y As Decimal) As Decimal
DivideXbyY = IIF(Y=0, 0, X / IIF(Y=0, 1, Y))
End Function

then I call it in the Table/Matrix

Code Snippet

=Code.DivideXbyY(Fields!amt.Value,Fields!tix.Value)

|||Thanks guys, your responses are very helpful and much appreciated.

Monday, February 13, 2012

average date of birth

Hi, folks.
I have a table with a column Date_of_birth.
I want to evaluate average date_of_birth by grouping on depts.
AVG function doesn't seem to work with datetime col. Plz help.Hi, folks.
I have a table with a column Date_of_birth.
I want to evaluate average date_of_birth by grouping on depts.
AVG function doesn't seem to work with datetime col. Plz help.

r u looking for average of age ??
coz avg of date of birth doesn't make any sense to me.|||yes, i want to evaluate average age on the basis of date of brith stored with in the column|||datediff ?|||AVG(datediff(yy,date_of_birth,getdate()))
this would work, i guess.
thanx|||How's about:

USE Northwind
GO

CREATE TABLE myTable99(Col1 datetime)
GO

INSERT INTO myTable99(Col1)
SELECT '10/24/1960' UNION ALL SELECT '10/24/1970' UNION ALL SELECT '10/24/1980'

SELECT CONVERT(datetime,AVG(CONVERT(float,Col1))) FROM myTable99
GO

DROP TABLE myTable99
GO|||Hi Brett, good morning.
That was what i've been lookin for.
How do we get the date back after it's converted into float.
SELECT convert(datetime,38174.0)
--------
2004-07-08 00:00:00.000
(1 row(s) affected)

Howdy!|||Here is a quick one:

************************************************** ****
SET NOCOUNT ON
/* Here is your table */
create table #working (Date_of_birth datetime)
insert into #working (Date_of_birth) VALUES ('1/1/1920')
insert into #working (Date_of_birth) VALUES ('1/6/1930')
insert into #working (Date_of_birth) VALUES ('1/17/1940')
DECLARE @.days_old int

/* Average days old */
SELECT @.days_old = AVG(datediff(dd, Date_of_birth, GETDATE()))
FROM #working

/* back it out now for average birth date, odd as that is :P */
SELECT dateadd(dd, -@.days_old, GETDATE())

drop table #working
SET NOCOUNT OFF
************************************************** ****

Hope it helps|||Aren't you concerned that cobalt sound a lot like COBOL?