SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


T-SQL help


T-SQL help

Author
Message
etirem
etirem
SSChasing Mays
SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)

Group: General Forum Members
Points: 621 Visits: 535
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


Tom Thomson
Tom Thomson
SSC Guru
SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)

Group: General Forum Members
Points: 50886 Visits: 13159
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

etirem
etirem
SSChasing Mays
SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)

Group: General Forum Members
Points: 621 Visits: 535
Thank you!!

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

.
etirem
etirem
SSChasing Mays
SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)

Group: General Forum Members
Points: 621 Visits: 535
But I also need to get the persons [code="sql"][WHERE age BETWEEN '4 Mths' AND '12 Yrs'/code]
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)

Group: General Forum Members
Points: 225092 Visits: 40420
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.

Cool
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)
etirem
etirem
SSChasing Mays
SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)

Group: General Forum Members
Points: 621 Visits: 535
Yes..This is an additional requirement. I need to retrieve the Persons based on the AGE range BETWEEN months and years.
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)

Group: General Forum Members
Points: 225092 Visits: 40420
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




Cool
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)
sqlbi.vvamsi
sqlbi.vvamsi
SSChasing Mays
SSChasing Mays (638 reputation)SSChasing Mays (638 reputation)SSChasing Mays (638 reputation)SSChasing Mays (638 reputation)SSChasing Mays (638 reputation)SSChasing Mays (638 reputation)SSChasing Mays (638 reputation)SSChasing Mays (638 reputation)

Group: General Forum Members
Points: 638 Visits: 412
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
etirem
etirem
SSChasing Mays
SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)SSChasing Mays (621 reputation)

Group: General Forum Members
Points: 621 Visits: 535
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


Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)

Group: General Forum Members
Points: 225092 Visits: 40420
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.

Cool
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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search