Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


T-SQL help


T-SQL help

Author
Message
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24171 Visits: 37936
Oh, and how is this information passed to query?

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
SSC Journeyman
SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)

Group: General Forum Members
Points: 80 Visits: 517
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
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24171 Visits: 37936
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




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)
Steven Willis
Steven Willis
SSC-Addicted
SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)

Group: General Forum Members
Points: 477 Visits: 1721
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>



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