Showing posts with label age. Show all posts
Showing posts with label age. Show all posts

Friday, February 24, 2012

Calculations at Run Time

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.
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 and displaying "Age"

Most people don't consider that they are a certain age in years until AFTER
the arrival of their birthdate. How can I calculate an "age" column in a tab
le from a birthdate by rounding down instead of up?
alter table member
add age
as datediff(year,birth_date,getdate())Hi,
Since datediff with year will round up the value. Please use the below code,
alter table member
add age
as convert(int,datediff(month,birth_date,ge
tdate())/12.0)
Thanks
Hari
MCDBA
"Jack Wachtler" <jack_wachtler@.comcast.net> wrote in message
news:08381C01-59BA-4517-852F-0B7445BBC66E@.microsoft.com...
> Most people don't consider that they are a certain age in years until
AFTER the arrival of their birthdate. How can I calculate an "age" column in
a table from a birthdate by rounding down instead of up?
> alter table member
> add age
> as datediff(year,birth_date,getdate())
>|||as DateDiff(year,birth_date,CURRENT_TIMESTA
MP) - CASE
WHEN Month(birth_date) > Month(CURRENT_TIMESTAMP) THEN 1
WHEN Month(birth_date) = Month(CURRENT_TIMESTAMP)
AND Day(birth_date) > Day(CURRENT_TIMESTAMP) THEN 1
ELSE 0 END
Gert-Jan
Jack Wachtler wrote:
> Most people don't consider that they are a certain age in years until AFTE
R the arrival of their birthdate. How can I calculate an "age" column in a t
able from a birthdate by rounding down instead of up?
> alter table member
> add age
> as datediff(year,birth_date,getdate())
(Please reply only to the newsgroup)|||A few common ones:
--#1
SELECT DATEDIFF(yy, @.dob, CURRENT_TIMESTAMP) -
CASE WHEN (MONTH(CURRENT_TIMESTAMP) * 100 +
DAY(CURRENT_TIMESTAMP)) <
(MONTH(@.dob)* 100 + DAY(@.dob))
THEN 1 ELSE 0 END
--#2
SELECT (CAST(CONVERT(CHAR(8),CURRENT_TIMESTAMP,
112) AS INT)
- CAST(CONVERT(CHAR(8), @.dob, 112) AS INT))/10000
--#3
SELECT YEAR(CURRENT_TIMESTAMP) - YEAR(@.dob) -
(CASE WHEN (MONTH(@.dob) > MONTH(CURRENT_TIMESTAMP))
OR (MONTH(@.dob) = MONTH(CURRENT_TIMESTAMP)
AND DAY(@.dob) > DAY(CURRENT_TIMESTAMP))
THEN 1 ELSE 0 END)
Anith|||http://www.aspfaq.com/2233
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
"Jack Wachtler" <jack_wachtler@.comcast.net> wrote in message
news:08381C01-59BA-4517-852F-0B7445BBC66E@.microsoft.com...
> Most people don't consider that they are a certain age in years until
AFTER the arrival of their birthdate. How can I calculate an "age" column in
a table from a birthdate by rounding down instead of up?
> alter table member
> add age
> as datediff(year,birth_date,getdate())
>|||We always use:
Set @.Age=datediff(YYYY,@.DOB,@.SOMEDATE)
-CASE WHEN
CAST(CONVERT(CHAR(6),@.DOB,101)+CAST(DATE
PART(YYYY,@.SOMEDATE) AS VARCHAR(4)) AS DATETIME)[co
lor=darkred]
>@.SOMEDATE THEN 1 ELSE 0 END[/color]|||Hari -- liked your simple solution the best and it works. Repeated below:
alter table member
add age
as convert(int,datediff(month,birth_date,ge
tdate())/12.0)
Thnx. -- Jack Wachtler|||Jack,
You might like it, but it don't work...
print convert(int,datediff(month,'1990-02-06','1991-02-05')/12.0)
gives me a 1 in QA - I believe this kid is one day short of a birthday.
With the problem of rounding going on in DATEDIFF, I see no way of diff'ing
the YEARS and seeing if the birthdate has physically passed by in the curren
t year, and adjusting appropriately.
Been doing it this fashion way before MS SQL came into the picture.
Steve
-- Jack Wachtler wrote: --
Hari -- liked your simple solution the best and it works. Repeated below:
alter table member
add age
as convert(int,datediff(month,birth_date,ge
tdate())/12.0)
Thnx. -- Jack Wachtler|||Anith Sen wrote:

>A few common ones:
>--#1
>SELECT DATEDIFF(yy, @.dob, CURRENT_TIMESTAMP) -
> CASE WHEN (MONTH(CURRENT_TIMESTAMP) * 100 +
> DAY(CURRENT_TIMESTAMP)) <
> (MONTH(@.dob)* 100 + DAY(@.dob))
> THEN 1 ELSE 0 END
>--#2
>SELECT (CAST(CONVERT(CHAR(8),CURRENT_TIMESTAMP,
112) AS INT)
> - CAST(CONVERT(CHAR(8), @.dob, 112) AS INT))/10000
>
>
Or for the lazy typist:
SELECT (0+CONVERT(CHAR(8),CURRENT_TIMESTAMP, 112)
- CONVERT(CHAR(8), @.dob, 112))/10000
SK

>--#3
>SELECT YEAR(CURRENT_TIMESTAMP) - YEAR(@.dob) -
> (CASE WHEN (MONTH(@.dob) > MONTH(CURRENT_TIMESTAMP))
> OR (MONTH(@.dob) = MONTH(CURRENT_TIMESTAMP)
> AND DAY(@.dob) > DAY(CURRENT_TIMESTAMP))
> THEN 1 ELSE 0 END)
>
>|||This query will throw an error if the date of birth is on 29th of
February, and SomeDate is not a leap year...
Gert-Jan
Steve Z wrote:
> We always use:
> Set @.Age=datediff(YYYY,@.DOB,@.SOMEDATE)
> -CASE WHEN
> CAST(CONVERT(CHAR(6),@.DOB,101)+CAST(DATE
PART(YYYY,@.SOMEDATE) AS VARCHAR(4
)) AS DATETIME)
(Please reply only to the newsgroup)

Calculating and displaying "Age"

Most people don't consider that they are a certain age in years until AFTER the arrival of their birthdate. How can I calculate an "age" column in a table from a birthdate by rounding down instead of up?
alter table membe
add ag
as datediff(year,birth_date,getdate()Hi,
Since datediff with year will round up the value. Please use the below code,
alter table member
add age
as convert(int,datediff(month,birth_date,getdate())/12.0)
Thanks
Hari
MCDBA
"Jack Wachtler" <jack_wachtler@.comcast.net> wrote in message
news:08381C01-59BA-4517-852F-0B7445BBC66E@.microsoft.com...
> Most people don't consider that they are a certain age in years until
AFTER the arrival of their birthdate. How can I calculate an "age" column in
a table from a birthdate by rounding down instead of up?
> alter table member
> add age
> as datediff(year,birth_date,getdate())
>|||as DateDiff(year,birth_date,CURRENT_TIMESTAMP) - CASE
WHEN Month(birth_date) > Month(CURRENT_TIMESTAMP) THEN 1
WHEN Month(birth_date) = Month(CURRENT_TIMESTAMP)
AND Day(birth_date) > Day(CURRENT_TIMESTAMP) THEN 1
ELSE 0 END
Gert-Jan
Jack Wachtler wrote:
> Most people don't consider that they are a certain age in years until AFTER the arrival of their birthdate. How can I calculate an "age" column in a table from a birthdate by rounding down instead of up?
> alter table member
> add age
> as datediff(year,birth_date,getdate())
--
(Please reply only to the newsgroup)|||A few common ones:
--#1
SELECT DATEDIFF(yy, @.dob, CURRENT_TIMESTAMP) -
CASE WHEN (MONTH(CURRENT_TIMESTAMP) * 100 +
DAY(CURRENT_TIMESTAMP)) <
(MONTH(@.dob)* 100 + DAY(@.dob))
THEN 1 ELSE 0 END
--#2
SELECT (CAST(CONVERT(CHAR(8),CURRENT_TIMESTAMP, 112) AS INT)
- CAST(CONVERT(CHAR(8), @.dob, 112) AS INT))/10000
--#3
SELECT YEAR(CURRENT_TIMESTAMP) - YEAR(@.dob) -
(CASE WHEN (MONTH(@.dob) > MONTH(CURRENT_TIMESTAMP))
OR (MONTH(@.dob) = MONTH(CURRENT_TIMESTAMP)
AND DAY(@.dob) > DAY(CURRENT_TIMESTAMP))
THEN 1 ELSE 0 END)
--
Anith|||http://www.aspfaq.com/2233
--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
"Jack Wachtler" <jack_wachtler@.comcast.net> wrote in message
news:08381C01-59BA-4517-852F-0B7445BBC66E@.microsoft.com...
> Most people don't consider that they are a certain age in years until
AFTER the arrival of their birthdate. How can I calculate an "age" column in
a table from a birthdate by rounding down instead of up?
> alter table member
> add age
> as datediff(year,birth_date,getdate())
>|||We always use
Set @.Age=datediff(YYYY,@.DOB,@.SOMEDATE
-CASE WHE
CAST(CONVERT(CHAR(6),@.DOB,101)+CAST(DATEPART(YYYY,@.SOMEDATE) AS VARCHAR(4)) AS DATETIME
>@.SOMEDATE THEN 1 ELSE 0 EN|||Hari -- liked your simple solution the best and it works. Repeated below:
alter table membe
add ag
as convert(int,datediff(month,birth_date,getdate())/12.0
Thnx. -- Jack Wachtler|||Jack
You might like it, but it don't work..
print convert(int,datediff(month,'1990-02-06','1991-02-05')/12.0
gives me a 1 in QA - I believe this kid is one day short of a birthday
With the problem of rounding going on in DATEDIFF, I see no way of diff'ing the YEARS and seeing if the birthdate has physically passed by in the current year, and adjusting appropriately
Been doing it this fashion way before MS SQL came into the picture
Stev
-- Jack Wachtler wrote: --
Hari -- liked your simple solution the best and it works. Repeated below:
alter table membe
add ag
as convert(int,datediff(month,birth_date,getdate())/12.0
Thnx. -- Jack Wachtler

Calculating Ages

When users register with my site they give me their DoB, how can I work out an age from it. I was thinkin about doing it from the year, but that isnt very accurate. Can anyone help? thanks si!

ps Id only want to work out how many years old they are. si!

You started on the right track. Subtract the DOB from Now.Date (or GETDATE()), and if the (DOB.Month >= Now.Month And DOB.Day > Now.Day) then this year's birthday is in the future, so subtract 1.

|||

Minus one date from the other and the result will be a TimeSpan. e.g.

Dim dAs New Date
Dim tAs New TimeSpan
d = System.DateTime.Today
t = d.Subtract("01/01/1950")
You can then display their age by using the relevant properties from the TimeSpan object.|||

Sorry can you tell me what that is in C# please. Si!

|||

There's no difference apart from how you declare the variables. e.g

DateTime d =new DateTime();TimeSpan t =new TimeSpan();d = System.DateTime.Today;t = d.Subtract("01/01/1950");
|||

This would work better, and you wouldn't have to calculate the years from days, which the timespan requires:

DateTime _today = DateTime.Now.Date;// strip the time.DateTime _DOB = DateTime.Parse(@."10/24/1980").Date;// strip the time.int _Age = 0;// get the difference in years._Age = _today.Year - _DOB.Year;//our user was born in October. If the current date is before October 24,// then the user hasn't had his birthday for this year. // 2007 - 1980 = 27, 27-1 = 26 years old.if ( _today.Month <= _DOB.Month && _today.Day <_DOB.Day ){//subtract 1 _Age -= 1;}//_Age now contains the correct age.
|||

Even though I used October above, the if statement applies to every possiblity. This is as accurate as you can get in calculating age, as it takes into account whether the person has reached their birthday yet this year.

Calculating Age...

Hello..
I am attempting to write a T-SQL script that will calculate a person's age
based on a date in the DOB field. The DOB field is a smalldatetime. I want
to return this calculated field in a result set. So for example, if the
date in the DOB field is 01/01/1900... I want the age to be returned as 105
(2005 - 1900). I am using SQL Server 2000
Any help would be appreciated...
Thanks,
Bretthttp://www.aspfaq.com/2233
This is my signature. It is a general reminder.
Please post DDL, sample data and desired results.
See http://www.aspfaq.com/5006 for info.
"Brett Davis" <bdavis123@.cox.net> wrote in message
news:OTREukdQFHA.2932@.TK2MSFTNGP09.phx.gbl...
> Hello..
> I am attempting to write a T-SQL script that will calculate a person's age
> based on a date in the DOB field. The DOB field is a smalldatetime. I
> want to return this calculated field in a result set. So for example, if
> the date in the DOB field is 01/01/1900... I want the age to be returned
> as 105 (2005 - 1900). I am using SQL Server 2000
> Any help would be appreciated...
> Thanks,
> Brett
>
>|||So say that this date is the birth date:
DROP FUNCTION TIMEMEMORIAL
GO
CREATE FUNCTION TIMEMEMORIAL
(
@.BIRTHDATE smalldatetime,
@.CURRENTDATE smalldatetime
)
RETURNS VARCHAR(200)
AS
BEGIN
DECLARE @.CURRENTYEAR CHAR(4)
DECLARE @.BIRTHYEAR CHAR(4)
DECLARE @.Result varchar(200)
SET @.BIRTHYEAR = CAST(YEAR(@.BIRTHDATE) as Char(4))
SET @.CURRENTYEAR = CAST(YEAR(@.CURRENTDATE) as Char(4))
Select @.result = CAST(datediff(yy,@.BIRTHDATE,@.CURRENTDATE
) AS varchar(10)) +
' (' + @.BIRTHYEAR + ' - ' + @.CURRENTYEAR + ')'
RETURN @.Result
END
Select dbo.TimeMemorial('20000101',getdate())
HTH, Jens Smeyer
http://www.sqlserver2005.de
--
"Brett Davis" <bdavis123@.cox.net> schrieb im Newsbeitrag
news:OTREukdQFHA.2932@.TK2MSFTNGP09.phx.gbl...
> Hello..
> I am attempting to write a T-SQL script that will calculate a person's age
> based on a date in the DOB field. The DOB field is a smalldatetime. I
> want to return this calculated field in a result set. So for example, if
> the date in the DOB field is 01/01/1900... I want the age to be returned
> as 105 (2005 - 1900). I am using SQL Server 2000
> Any help would be appreciated...
> Thanks,
> Brett
>
>|||See :
http://groups.google.ca/groups?selm...FTNGP09.phx.gbl
Anith|||I think this is easier;
checking for VALID data...
Making assumption that your want it from TODAY
select
case
when ISDATE(convert(char(15),[DATEFIELD])) = 0 then null
else
datediff(year, convert(datetime,([DATEFIELD])),getdate(
))
end
"Brett Davis" wrote:

> Hello..
> I am attempting to write a T-SQL script that will calculate a person's age
> based on a date in the DOB field. The DOB field is a smalldatetime. I wa
nt
> to return this calculated field in a result set. So for example, if the
> date in the DOB field is 01/01/1900... I want the age to be returned as 10
5
> (2005 - 1900). I am using SQL Server 2000
> Any help would be appreciated...
> Thanks,
> Brett
>
>
>|||On Fri, 15 Apr 2005 10:29:04 -0700, Jim Yurt wrote:

>I think this is easier;
>checking for VALID data...
>Making assumption that your want it from TODAY
>select
>case
>when ISDATE(convert(char(15),[DATEFIELD])) = 0 then null
>else
>datediff(year, convert(datetime,([DATEFIELD])),getdate(
))
>
>end
Hi Jim,
But that will return the number of year boundaries (dec 31 / Jan 1)
since the birth date, not the age!
SELECT DATEDIFF(year, '20041231', '20050101'),
DATEDIFF(year, '20040101', '20051231')
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||Try This:
If @.DOB is datetime date of birth, and
@.CurDT is a dateTime with current date, then
Select Year(@.CurDT) - Year(@.DOB) -
Case When Month(@.CurDT) < Month(@.DOB) Then 1
When Month(@.CurDT) > Month(@.DOB) Then 0
When Day(@.CurDT) < Day(@.DOB) Then 1
Else 0 End
"Brett Davis" wrote:

> Hello..
> I am attempting to write a T-SQL script that will calculate a person's age
> based on a date in the DOB field. The DOB field is a smalldatetime. I wa
nt
> to return this calculated field in a result set. So for example, if the
> date in the DOB field is 01/01/1900... I want the age to be returned as 10
5
> (2005 - 1900). I am using SQL Server 2000
> Any help would be appreciated...
> Thanks,
> Brett
>
>
>

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

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,
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

Calculating Age based on Document Date in months

Hi, I'm trying to write a formula that will calculate a patient's age based on the visit date and then converting it into months. Anyone know how to do that?
The variables would be:
{DOCUMENT.CLINICALDATE}
{PERSON.DATEOFBIRTH}The next will calculate age in years
({DOCUMENT.CLINICALDATE} - {PERSON.DATEOFBIRTH}) / 365

The next will calculate age in months
({DOCUMENT.CLINICALDATE} - {PERSON.DATEOFBIRTH}) / 365 * 12