Friday, February 24, 2012
Calculations at Run Time
I am writting a report & want to calculate %age difference in last year &
current year's sales.
how do I write the expression or will have to write it in the SP I'm using
to run the report.
The problem is, I need to check the previous years sales value only if it is
greater than 0, then calculate the %age difference so it is something like
this
if prevYearSales > 0 then
currYearSales - prevYearSales / prevYearSales.
ThanksYou do it in an SP or as the following report expression
=IIF(Fields!prevYearSales.Value > 0,
Fields!currYearSales.Value - FIelds!prevYearSales.Value /
Fields!prevYearSales.Value,0)
It sets other values to 0 (the last expression)
--
Wayne Snyder MCDBA, SQL Server MVP
Mariner, Charlotte, NC
I support the Professional Association for SQL Server ( PASS) and it''s
community of SQL Professionals.
"Vishal" wrote:
> Hi,
> I am writting a report & want to calculate %age difference in last year &
> current year's sales.
> how do I write the expression or will have to write it in the SP I'm using
> to run the report.
> The problem is, I need to check the previous years sales value only if it is
> greater than 0, then calculate the %age difference so it is something like
> this
> if prevYearSales > 0 then
> currYearSales - prevYearSales / prevYearSales.
> Thanks
>
>
Sunday, February 12, 2012
calculating age using sql server function
Hi ya,
I have a table which will be having DOB and I want to calculate the current age of persons, i want to save it into currentage as default.
I have searched over the interent and find some solutions but in my database the DOB is stored as = 'dd/mm/yyyy' and none of the solutions work on this.
Any ideas?
Prince:
I assume by your post that your DOB is stored as a string. Try:
select datediff (year, convert (datetime, '07/15/1908'), getdate())
|||Dave
Mugambo wrote:
Prince:
I assume by your post that your DOB is stored as a string. Try:
select datediff (year, convert (datetime, '07/15/1908'), getdate())
Dave
Hello Dave,
No you got me wrong, the DOB is stored as Datetime in Sql 2005, the other thing is that datediff is not going to calculate right and thirdly my dates are stored as datetime but in dd/mm/yyyy format.
You can try it by yourself.
|||
First, create this user defined function.
ALTER function [dbo].[fn_GetAge]
(@.in_DOB AS datetime,@.now as datetime)
returns int
as
begin
DECLARE @.age int
IF cast(datepart(m,@.now) as int) > cast(datepart(m,@.in_DOB) as int)
SET @.age = cast(datediff(yyyy,@.in_DOB,@.now) as int)
else
IF cast(datepart(m,@.now) as int) = cast(datepart(m,@.in_DOB) as int)
IF datepart(d,@.now) >= datepart(d,@.in_DOB)
SET @.age = cast(datediff(yyyy,@.in_DOB,@.now) as int)
ELSE
SET @.age = cast(datediff(yyyy,@.in_DOB,@.now) as int) -1
ELSE
SET @.age = cast(datediff(yyyy,@.in_DOB,@.now) as int) - 1
RETURN @.age
end
-
Then, to use it...
Select DOB, dbo.fn_GetAge(DOB, GetDate()) As Age From Table
Since all calculations are based on DateTime's, your formatting shouldn't matter
|||
thanks mastros,
it works really great.
Calculating Age in Yrs & Months
I need to add Year & Months to a persons age from a
datetime (DoB) field. The current script I have (as below)
can calculate only Years but NOT months. Does anyone have
any idea or a ready script that I can use which will give
me months & years for that person using the Dob field?
CASE
WHEN DATEADD(year, DATEDIFF (year, p1.DOB, getdate()),
p1.DOB) > getdate()
THEN DATEDIFF (year, p1.DOB, getdate()) - 1
ELSE DATEDIFF (year, p1.DOB, getdate())
END as 'Age'
Thanks much in advance,
JoshiSee if this helps:
[url]http://www.alegsa.com.ar/Visitas/i45/Calculating%20yrs%20&%20mths%20for%20age.php[
/url]
AMB
"J. Joshi" wrote:
> Hello all,
> I need to add Year & Months to a persons age from a
> datetime (DoB) field. The current script I have (as below)
> can calculate only Years but NOT months. Does anyone have
> any idea or a ready script that I can use which will give
> me months & years for that person using the Dob field?
> CASE
> WHEN DATEADD(year, DATEDIFF (year, p1.DOB, getdate()),
> p1.DOB) > getdate()
> THEN DATEDIFF (year, p1.DOB, getdate()) - 1
> ELSE DATEDIFF (year, p1.DOB, getdate())
> END as 'Age'
> Thanks much in advance,
> Joshi
>|||Substracting the integer representation of the date of birth's month value
from the integer representation of current month value will give you the
number of months you are looking for.
Anith