Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

T-SQL help Expand / Collapse
Author
Message
Posted Friday, May 3, 2013 4:06 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 10:48 PM
Points: 74, Visits: 386
For the below SQL code, I need the output as:
person dob Age
Frank 3/20/1990 23 Years
Joey 12/31/2000 12 Years
Sue 2/6/2012 1 Years
Mary 4/1/2013 1 Months
Bil 5/3/2012 1 Years
Bob 1/1/2013 4 Months
Will 10/1/2012 7 Months

declare @people as table (
person varchar(20),
dob date
)

declare @SDate as date = '5/3/2013';

insert into @people(person,dob)
values ('Frank','3/20/1990'),
('Joey','12/31/2000'),
('Sue','2/6/2012'),
('Mary','4/1/2013'),
('Bil','5/3/2012'),
('Bob','1/1/2013'),
('Will','10/1/2012')

SELECT person,
dob,
DATEDIFF(YEAR, dob, @SDate) -
CASE WHEN((MONTH(dob)*100 + DAY(dob)) > (MONTH(@SDate)*100 + DAY(@SDate))) THEN 1 ELSE 0 END age
FROM @people

Post #1449367
Posted Friday, May 3, 2013 6:52 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 5:32 PM
Points: 8,738, Visits: 9,284
The natural thing to do is calculate the age in years in a cte, then calculate teh age in months for cases where the age in years is 0. That's pretty straight forward:
with years(person,dob,age_y) as 
(select person,
dob,
DATEDIFF(YEAR, dob, @SDate) -
CASE WHEN((MONTH(dob)*100 + DAY(dob)) > (MONTH(@SDate)*100 + DAY(@SDate)))
THEN 1
ELSE 0
END as age_y
FROM @people)
select person, dob,
case when age_y > 0
then LTRIM(STR(age_y)) + ' Yrs'
else LTRIM(STR(DATEDIFF(MM,dob,@SDate) -
case when DATEPART(DD,dob)>DATEPART(DD,@SDATE) then 1 else 0
end)) + ' Mths'
end as age
from years



Tom
Post #1449377
Posted Saturday, May 4, 2013 10:15 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 10:48 PM
Points: 74, Visits: 386
Thank you!!

But I also need to get the persons
WHERE age BETWEEN '4 Mths' AND '12 Yrs'

.
Post #1449437
Posted Monday, May 6, 2013 10:47 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 10:48 PM
Points: 74, Visits: 386
But I also need to get the persons [code="sql"][WHERE age BETWEEN '4 Mths' AND '12 Yrs'/code]
Post #1449798
Posted Monday, May 6, 2013 10:53 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 7:50 PM
Points: 23,299, Visits: 32,039
etirem (5/4/2013)
Thank you!!

But I also need to get the persons
WHERE age BETWEEN '4 Mths' AND '12 Yrs'

.


Is this an additional requirement to only display individuals that are between 4 months old and 12 years old? I didn't see that in your original post.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1449800
Posted Monday, May 6, 2013 11:29 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 10:48 PM
Points: 74, Visits: 386
Yes..This is an additional requirement. I need to retrieve the Persons based on the AGE range BETWEEN months and years.
Post #1449815
Posted Monday, May 6, 2013 11:49 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 7:50 PM
Points: 23,299, Visits: 32,039
This:


declare @people as table (
person varchar(20),
dob date
)

declare @SDate as date = '5/3/2013';

insert into @people(person,dob)
values ('Frank','3/20/1990'),
('Joey','12/31/2000'),
('Sue','2/6/2012'),
('Mary','4/1/2013'),
('Bil','5/3/2012'),
('Bob','1/1/2013'),
('Will','10/1/2012')

SELECT person,
dob,
case when datediff(month, dob, getdate()) - case when dateadd(month, datediff(month, dob, getdate()), dob) > getdate() then 1 else 0 end < 12
then cast(datediff(month, dob, getdate()) - case when dateadd(month, datediff(month, dob, getdate()), dob) > getdate() then 1 else 0 end as varchar) + ' Months'
else cast((datediff(month, dob, getdate()) - case when dateadd(month, datediff(month, dob, getdate()), dob) > getdate() then 1 else 0 end) / 12 as varchar) + ' Years'
end as age
FROM @people
WHERE case when datediff(month, dob, getdate()) - case when dateadd(month, datediff(month, dob, getdate()), dob) > getdate() then 1 else 0 end < 12
then datediff(month, dob, getdate()) - case when dateadd(month, datediff(month, dob, getdate()), dob) > getdate() then 1 else 0 end
else ((datediff(month, dob, getdate()) - case when dateadd(month, datediff(month, dob, getdate()), dob) > getdate() then 1 else 0 end) / 12) * 12
end between 4 and 144





Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1449821
Posted Monday, May 6, 2013 12:12 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, August 28, 2014 9:05 AM
Points: 54, Visits: 359
This also should work..

declare @people as table (
person varchar(20),
dob date
)

declare @SDate date
set @SDate= '5/3/2013'

insert into @people(person,dob)
values ('Frank','3/20/1990'),
('Joey','12/31/2000'),
('Sue','2/6/2012'),
('Mary','4/1/2013'),
('Bil','5/3/2012'),
('Bob','1/1/2013'),
('Will','10/1/2012')

SELECT person,
dob,
case when DATEDIFF(MONTH,dob,@SDate)/12>0 then convert(varchar,DATEDIFF(YEAR,dob,@SDate))+ ' Years' else convert(varchar,DATEDIFF(MONTH,dob,@SDate)) + ' Months' end as age
FROM @people

where DATEDIFF(MONTH,dob,@SDate)>=4 and DATEDIFF(YEAR,dob,@SDate)<=12


Post #1449831
Posted Monday, May 6, 2013 1:41 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 10:48 PM
Points: 74, Visits: 386
But I have different scenarios to get the persons retrieved based on the parameters for BEGIN AGE and END AGE in WHERE Clause:
Like :
SELECT person, dob, 
case when DATEDIFF(MONTH,dob,@SDate)/12>0 then convert(varchar,DATEDIFF(YEAR,dob,@SDate))+ ' Years' else convert(varchar,DATEDIFF(MONTH,dob,@SDate)) + ' Months' end as age
FROM @people
where --DATEDIFF(MONTH,dob,@SDate)>=1 and DATEDIFF(YEAR,dob,@SDate)<=7
--Age BETWEEN @BEGINAge AND @ENDAge
Query1: Age BETWEEN 1 Months AND 1 Years
Query2: Age BETWEEN 4 Months AND 13 Years
Query3: Age BETWEEN 1 Years AND 23 Years

Post #1449862
Posted Monday, May 6, 2013 2:07 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 7:50 PM
Points: 23,299, Visits: 32,039
etirem (5/6/2013)
But I have different scenarios to get the persons retrieved based on the parameters for BEGIN AGE and END AGE in WHERE Clause:
Like :
SELECT person, dob, 
case when DATEDIFF(MONTH,dob,@SDate)/12>0 then convert(varchar,DATEDIFF(YEAR,dob,@SDate))+ ' Years' else convert(varchar,DATEDIFF(MONTH,dob,@SDate)) + ' Months' end as age
FROM @people
where --DATEDIFF(MONTH,dob,@SDate)>=1 and DATEDIFF(YEAR,dob,@SDate)<=7
--Age BETWEEN @BEGINAge AND @ENDAge
Query1: Age BETWEEN 1 Months AND 1 Years
Query2: Age BETWEEN 4 Months AND 13 Years
Query3: Age BETWEEN 1 Years AND 23 Years



Is this the last change in your requirements?

Also, looking above it looks like this even changes your last change for 4 months to 12 years.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1449872
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse