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 Monday, May 6, 2013 2:09 PM


SSC-Insane

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

Group: General Forum Members
Last Login: Today @ 9:58 AM
Points: 23,034, Visits: 31,558
Oh, and how is this information passed to query?



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 #1449873
Posted Monday, May 6, 2013 2:22 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Yesterday @ 4:37 PM
Points: 70, Visits: 355
Sorry, this is the last requirement.

The values are passed to the query from a drop-down list from SSRS report.
Both drop down lists are sames values:
1Months
2Months
.
.
12Months
1Years
2Years
3Years
.
100Years

BeginAge: 2Months EndAge: 48Years
Post #1449877
Posted Monday, May 6, 2013 3:05 PM


SSC-Insane

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

Group: General Forum Members
Last Login: Today @ 9:58 AM
Points: 23,034, Visits: 31,558
Maybe this:


declare @StartPeriod varchar(10),
@EndPeriod varchar(10);

select @StartPeriod = '4Months', @EndPeriod = '12Years';

select
cast(substring(@StartPeriod, 1, patindex('%[^0-9]%',@StartPeriod) - 1) as int) * case when patindex('%Months%',@StartPeriod) > 0 then 1 when patindex('%Years%',@StartPeriod) > 0 then 12 else 0 end,
cast(substring(@EndPeriod, 1, patindex('%[^0-9]%',@EndPeriod) - 1) as int) * case when patindex('%Months%',@EndPeriod) > 0 then 1 when patindex('%Years%',@EndPeriod) > 0 then 12 else 0 end

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

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 cast(substring(@StartPeriod, 1, patindex('%[^0-9]%',@StartPeriod) - 1) as int) * case when patindex('%Months%',@StartPeriod) > 0 then 1 when patindex('%Years%',@StartPeriod) > 0 then 12 else 0 end
and cast(substring(@EndPeriod, 1, patindex('%[^0-9]%',@EndPeriod) - 1) as int) * case when patindex('%Months%',@EndPeriod) > 0 then 1 when patindex('%Years%',@EndPeriod) > 0 then 12 else 0 end



select @StartPeriod = '3Years', @EndPeriod = '50Years';

select
cast(substring(@StartPeriod, 1, patindex('%[^0-9]%',@StartPeriod) - 1) as int) * case when patindex('%Months%',@StartPeriod) > 0 then 1 when patindex('%Years%',@StartPeriod) > 0 then 12 else 0 end,
cast(substring(@EndPeriod, 1, patindex('%[^0-9]%',@EndPeriod) - 1) as int) * case when patindex('%Months%',@EndPeriod) > 0 then 1 when patindex('%Years%',@EndPeriod) > 0 then 12 else 0 end


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 cast(substring(@StartPeriod, 1, patindex('%[^0-9]%',@StartPeriod) - 1) as int) * case when patindex('%Months%',@StartPeriod) > 0 then 1 when patindex('%Years%',@StartPeriod) > 0 then 12 else 0 end
and cast(substring(@EndPeriod, 1, patindex('%[^0-9]%',@EndPeriod) - 1) as int) * case when patindex('%Months%',@EndPeriod) > 0 then 1 when patindex('%Years%',@EndPeriod) > 0 then 12 else 0 end





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 #1449895
Posted Monday, May 6, 2013 7:59 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
Try this (function posted below) the script:


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
*
FROM
@People p
CROSS APPLY
dbo.itvfCalcAge(p.dob,@SDate)



CREATE FUNCTION dbo.itvfCalcAge
(
@StartDate DATETIME
,@EndDate DATETIME
)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN
(
-- CREATE DATE DEFAULTS

WITH cteDefaults
(
CurrDate,
CurrYrStartDate,
FirstDOMCurrMonth,
LastDOMCurrMonth,
FirstDOMStartDate,
LastDOMEndDate,
FirstDOYCurrYr,
LastDOYCurrYr
)
AS
(
SELECT
GETDATE() AS CurrDate
,(CASE
WHEN DATEDIFF(DAY,DATEADD(YEAR,(YEAR(GETDATE())-YEAR(@StartDate)),@StartDate),GETDATE()) > 0
THEN DATEADD(YEAR,(YEAR(GETDATE())-YEAR(@StartDate)),@StartDate)
ELSE
DATEADD(YEAR,-1,DATEADD(YEAR,(YEAR(GETDATE())-YEAR(@StartDate)),@StartDate))
END)
AS CurrYrStartDate
,DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE()),0) AS FirstDOMCurrMonth
,DATEADD(MILLISECOND,-3,DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE())+1,0)) AS LastDOMCurrMonth
,DATEADD(MONTH,DATEDIFF(MONTH,0,@StartDate),0) AS FirstDOMStartDate
,DATEADD(MILLISECOND,-3,DATEADD(MONTH,DATEDIFF(MONTH,0,@EndDate)+1,0)) AS LastDOMEndDate
,DATEADD(YEAR,DATEDIFF(YEAR,0,GETDATE()),0) AS FirstDOYCurrYr
,DATEADD(MILLISECOND,-3,DATEADD(YEAR,DATEDIFF(YEAR,0,GETDATE())+1,0)) AS LastDOYCurrYr
),

-- DETERMINE THE MOST RECENT ANNIVERSARY DATE AND NEXT ANNIVERSARY DATE.
-- THIS WILL BE USED FOR DETERMINING THE DECIMAL PART OF AGE.

cteAnniversaries (LastAnniversary,NextAnniversary)
AS
(
SELECT
CurrYrStartDate AS LastAnniversary
,DATEADD(YEAR,1,CurrYrStartDate) AS NextAnniversary
FROM
cteDefaults
),

-- DETERMINE THE NUMBER OF DAYS BETWEEN THE ANNIVERSARY DATES.
-- THIS WILL TAKE INTO ACCOUNT LEAP YEARS

cteDaysPerYear(NumDaysCurrYear,TotalYears,TotalMonths,TotalDays,RecentAnniversary)
AS
(
SELECT
DATEDIFF(DAY, a.LastAnniversary, a.NextAnniversary)
AS NumDaysCurrYear
,DATEDIFF(YEAR,@StartDate,@EndDate)
AS TotalYears
,DATEDIFF(MONTH,a.LastAnniversary,@EndDate)
AS TotalMonths
,(DATEDIFF(DAY,DATEADD(DAY,-(DAY(@EndDate)-1),@EndDate),@EndDate))+1
AS TotalDays
,a.LastAnniversary
FROM
cteAnniversaries a
)

-- NOW GET THE DECIMAL PART AND ADD TOGETHER

SELECT
(CASE
WHEN
dpy.TotalYears
+ CAST(DATEDIFF(DAY,dpy.RecentAnniversary,@EndDate) AS NUMERIC(6,2))
/ ISNULL(CAST(dpy.NumDaysCurrYear AS NUMERIC(6,2)),1) > 0
THEN
dpy.TotalYears
+ CAST(ROUND(CAST(DATEDIFF(DAY,dpy.RecentAnniversary,@EndDate) AS NUMERIC(6,2))
/ ISNULL(CAST(dpy.NumDaysCurrYear AS NUMERIC(6,2)),1),4) AS NUMERIC(6,4))
ELSE 0
END) AS TotalAge
,(CASE
WHEN dpy.TotalYears < 0 THEN 0
ELSE dpy.TotalYears
END) AS Years
,(CASE
WHEN dpy.TotalMonths < 0 THEN 0
ELSE dpy.TotalMonths
END) AS Months
,(CASE
WHEN dpy.TotalDays < 0 THEN 0
ELSE dpy.TotalDays
END) AS Days
FROM
cteDaysPerYear AS dpy
)
GO


Output


<?xml version="1.0" ?>
<RESULTS1>
<RECORD>
<person>Frank</person>
<dob>1990-03-20</dob>
<TotalAge>23.1205</TotalAge>
<Years>23</Years>
<Months>2</Months>
<Days>3</Days>
</RECORD>
<RECORD>
<person>Joey</person>
<dob>2000-12-31</dob>
<TotalAge>13.3370</TotalAge>
<Years>13</Years>
<Months>5</Months>
<Days>3</Days>
</RECORD>
<RECORD>
<person>Sue</person>
<dob>2012-02-06</dob>
<TotalAge>1.2356</TotalAge>
<Years>1</Years>
<Months>3</Months>
<Days>3</Days>
</RECORD>
<RECORD>
<person>Mary</person>
<dob>2013-04-01</dob>
<TotalAge>0.0877</TotalAge>
<Years>0</Years>
<Months>1</Months>
<Days>3</Days>
</RECORD>
<RECORD>
<person>Bil</person>
<dob>2012-05-03</dob>
<TotalAge>1.0000</TotalAge>
<Years>1</Years>
<Months>0</Months>
<Days>3</Days>
</RECORD>
<RECORD>
<person>Bob</person>
<dob>2013-01-01</dob>
<TotalAge>0.3342</TotalAge>
<Years>0</Years>
<Months>4</Months>
<Days>3</Days>
</RECORD>
<RECORD>
<person>Will</person>
<dob>2012-10-01</dob>
<TotalAge>1.5863</TotalAge>
<Years>1</Years>
<Months>7</Months>
<Days>3</Days>
</RECORD>
</RESULTS1>


Post #1449957
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse