dbTalk Databases Forums  

Help Needed For Date field (Age) Calculation In SQl query

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Help Needed For Date field (Age) Calculation In SQl query in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Dinesh
 
Posts: n/a

Default Help Needed For Date field (Age) Calculation In SQl query - 08-30-2007 , 05:18 AM






Hi experts,

I am working on SQL server 2005 reporting services and i am getting a
problem in writting a query.

Situation is given below.

There is one table in database Named Child

Now i have to find the All childrens whoes Age is 13 years Base on
Some given parameter.

If User select Augus 2007 then It has to calculate the Childs who born
in August 1994 And if he select September Then query

should show only those child Who born in September 1994 and so
on..... And use can select another year month also like

August 2009 ...


I am writting the following query

Select Child_Name, DOb from Child
where ((CONVERT(DateTime, A.Date_Of_Birth, 103) >= @ Parameter1
And (CONVERT(DateTime, A.Date_Of_Birth, 103) <= @Parameter2)

If i know already month and year then i can write easily parameter1
and parameter2 But since these are comming from user so i m not
finding how to handle this.


Now please suggest me what i have to write in Where statement I think
a lot but not getting any idea about it.



Any help wil be appriciated.

Regards
Dinesh


Reply With Quote
  #2  
Old   
Roy Harvey
 
Posts: n/a

Default Re: Help Needed For Date field (Age) Calculation In SQl query - 08-30-2007 , 08:36 AM






If I understand correctly, you want to select on a specific month of
birth that is 13 years prior to a month provided as a starting point.

create table Born (Name varchar(20), dob datetime)

INSERT Born values('Nancy', '19940704')
INSERT Born values('Ralph', '19940804')
INSERT Born values('Edgar', '19940904')

declare @d datetime
set @d = '20070801'

SELECT @d, *
FROM Born
WHERE DATEDIFF(month,dob,@d) = (13 * 12)

DROP TABLE Born

Roy Harvey
Beacon Falls, CT

On Thu, 30 Aug 2007 03:18:34 -0700, Dinesh <dinesht15 (AT) gmail (DOT) com>
wrote:

Quote:
Hi experts,

I am working on SQL server 2005 reporting services and i am getting a
problem in writting a query.

Situation is given below.

There is one table in database Named Child

Now i have to find the All childrens whoes Age is 13 years Base on
Some given parameter.

If User select Augus 2007 then It has to calculate the Childs who born
in August 1994 And if he select September Then query

should show only those child Who born in September 1994 and so
on..... And use can select another year month also like

August 2009 ...


I am writting the following query

Select Child_Name, DOb from Child
where ((CONVERT(DateTime, A.Date_Of_Birth, 103) >= @ Parameter1
And (CONVERT(DateTime, A.Date_Of_Birth, 103) <= @Parameter2)

If i know already month and year then i can write easily parameter1
and parameter2 But since these are comming from user so i m not
finding how to handle this.


Now please suggest me what i have to write in Where statement I think
a lot but not getting any idea about it.



Any help wil be appriciated.

Regards
Dinesh

Reply With Quote
  #3  
Old   
Dinesh
 
Posts: n/a

Default Re: Help Needed For Date field (Age) Calculation In SQl query - 08-31-2007 , 03:17 AM



On Aug 30, 6:36 pm, Roy Harvey <roy_har... (AT) snet (DOT) net> wrote:
Quote:
If I understand correctly, you want to select on a specific month of
birth that is 13 years prior to a month provided as a starting point.

create table Born (Name varchar(20), dob datetime)

INSERT Born values('Nancy', '19940704')
INSERT Born values('Ralph', '19940804')
INSERT Born values('Edgar', '19940904')

declare @d datetime
set @d = '20070801'

SELECT @d, *
FROM Born
WHERE DATEDIFF(month,dob,@d) = (13 * 12)

DROP TABLE Born

Roy Harvey
Beacon Falls, CT

On Thu, 30 Aug 2007 03:18:34 -0700, Dinesh <dinesh... (AT) gmail (DOT) com
wrote:



Hi experts,

I am working on SQL server 2005 reporting services and i am getting a
problem in writting a query.

Situation is given below.

There is one table in database Named Child

Now i have to find the All childrens whoes Age is 13 years Base on
Some given parameter.

If User select Augus 2007 then It has to calculate the Childs who born
in August 1994 And if he select September Then query

should show only those child Who born in September 1994 and so
on..... And use can select another year month also like

August 2009 ...

I am writting the following query

Select Child_Name, DOb from Child
where ((CONVERT(DateTime, A.Date_Of_Birth, 103) >= @ Parameter1
And (CONVERT(DateTime, A.Date_Of_Birth, 103) <= @Parameter2)

If i know already month and year then i can write easily parameter1
and parameter2 But since these are comming from user so i m not
finding how to handle this.

Now please suggest me what i have to write in Where statement I think
a lot but not getting any idea about it.

Any help wil be appriciated.

Regards
Dinesh- Hide quoted text -

- Show quoted text -
Thanks Roy Harvey
Yes for your reply. I got my solution with your help.
Regards
Dinesh



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.