Thursday, March 29, 2012

Date question, Can I get Q1, of 2006 begin and end dates?

I would like to get the first and last day of a quarter by passing in todays
date.
I can get the quarter were in with
select (DATEPART(Quarter,GetDate()))
Now I want to get the day the quarter started (Min day of the quarter) and
the Last day of the quarter (Max day of the quarter) i.e. 4/1/06 and 6/30/06
.
Tall MikeUse a calendar table.
http://www.aspfaq.com/2519
"TallMike" <TallMike@.discussions.microsoft.com> wrote in message
news:62668B64-9559-46FA-98CB-03E781C4B55F@.microsoft.com...
>I would like to get the first and last day of a quarter by passing in
>todays
> date.
> I can get the quarter were in with
> select (DATEPART(Quarter,GetDate()))
> Now I want to get the day the quarter started (Min day of the quarter)
> and
> the Last day of the quarter (Max day of the quarter) i.e. 4/1/06 and
> 6/30/06.
>
> --
> Tall Mike|||Either of these sring expressions can easilty be converted to a
datetime. I figured that they would be more easily understood without
wrapping them in that.
select convert(char(4),datepart(year,getdate())
) +
CASE DATEPART(Quarter,GetDate())
WHEN 1 THEN '0101'
WHEN 2 THEN '0401'
WHEN 3 THEN '0701'
WHEN 4 THEN '1001'
END as QuarterStartString,
convert(char(4),datepart(year,getdate())
) +
CASE DATEPART(Quarter,GetDate())
WHEN 1 THEN '0331'
WHEN 2 THEN '0630'
WHEN 3 THEN '0930'
WHEN 4 THEN '1231'
END as QuarterEndString
Roy Harvey
Beacon Falls, CT
On Wed, 3 May 2006 13:36:02 -0700, TallMike
<TallMike@.discussions.microsoft.com> wrote:
>I would like to get the first and last day of a quarter by passing in today
s
>date.
>I can get the quarter were in with
>select (DATEPART(Quarter,GetDate()))
>Now I want to get the day the quarter started (Min day of the quarter) and
>the Last day of the quarter (Max day of the quarter) i.e. 4/1/06 and 6/30/06.[/colo
r]|||Check this out, it works
select DATEADD(qq, DATEDIFF(qq,0,getdate()), 0) as 'Last Q ended'
select DATEADD(qq, DATEDIFF(qq,0,getdate())-1, 0) as 'Last Q started'
Tall Mike
"Roy Harvey" wrote:

> Either of these sring expressions can easilty be converted to a
> datetime. I figured that they would be more easily understood without
> wrapping them in that.
> select convert(char(4),datepart(year,getdate())
) +
> CASE DATEPART(Quarter,GetDate())
> WHEN 1 THEN '0101'
> WHEN 2 THEN '0401'
> WHEN 3 THEN '0701'
> WHEN 4 THEN '1001'
> END as QuarterStartString,
> convert(char(4),datepart(year,getdate())
) +
> CASE DATEPART(Quarter,GetDate())
> WHEN 1 THEN '0331'
> WHEN 2 THEN '0630'
> WHEN 3 THEN '0930'
> WHEN 4 THEN '1231'
> END as QuarterEndString
> Roy Harvey
> Beacon Falls, CT
> On Wed, 3 May 2006 13:36:02 -0700, TallMike
> <TallMike@.discussions.microsoft.com> wrote:
>
>|||On Wed, 3 May 2006 15:25:02 -0700, TallMike
<TallMike@.discussions.microsoft.com> wrote:

>Check this out, it works
>select DATEADD(qq, DATEDIFF(qq,0,getdate()), 0) as 'Last Q ended'
>select DATEADD(qq, DATEDIFF(qq,0,getdate())-1, 0) as 'Last Q started'
Very nice! It came pretty close, but the Last Q ended was April 1,
not March 31.
But it didn't need much work.
select DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)-1 as 'Last Q ended'
select DATEADD(qq, DATEDIFF(qq,0,getdate())-1, 0) as 'Last Q started'
Roy Harvey
Beacon Falls, CT

No comments:

Post a Comment