Tuesday, March 20, 2012

Calculate fiscal periods (T-SQL)

Hi Experts,
I would like to be able to take in a year and figure out its 12 month period
based on Sunday as starting day of the 1st week of that month and each perio
d
has 4 weeks.
For example:
Input: 2007
Output:
P1: 12/31/2006 - 1/27/2007
P2: 1/28/2007 - 2/24/2007
P3: 2/25/2007 - 3/31/2007
P4: 4/1/2007 - 4/28/2007
P5: 4/29/2007 - 5/26/2007
P6: 5/27/2007 - 6/30/2007
P7: 7/1/2007 - 7/28/2007
P8: 7/29/2007 - 8/25/2007
.
.
.
P12: 11/25/2007 -12/29/2007
P1 starts on 12/31/2006 because that falls on Sunday of the 1st week of
January/2007. P1 ends on 1/27/2007 because that's last day (Saturday) of 4th
week
and so on
My accounting dept folks call this fiscal period.
Anyone out there have done this or know how to do this type of calculation
using T-SQL.
I appreciate so much for any help.
Thanhexamnotes <ThanhNguyen@.discussions.microsoft.com>
wrote in news:ABAF7E37-0534-42EB-95A6-23E95963D54A@.microsoft.com:

> Hi Experts,
> I would like to be able to take in a year and figure out its 12 month
> period based on Sunday as starting day of the 1st week of that month
> and each period has 4 weeks.
> For example:
> Input: 2007
> Output:
> P1: 12/31/2006 - 1/27/2007
> P2: 1/28/2007 - 2/24/2007
> P3: 2/25/2007 - 3/31/2007
> P4: 4/1/2007 - 4/28/2007
> P5: 4/29/2007 - 5/26/2007
> P6: 5/27/2007 - 6/30/2007
> P7: 7/1/2007 - 7/28/2007
> P8: 7/29/2007 - 8/25/2007
> .
> .
> .
> P12: 11/25/2007 -12/29/2007
>
> P1 starts on 12/31/2006 because that falls on Sunday of the 1st week
> of January/2007. P1 ends on 1/27/2007 because that's last day
> (Saturday) of 4th week
> and so on
> My accounting dept folks call this fiscal period.
> Anyone out there have done this or know how to do this type of
> calculation using T-SQL.
> I appreciate so much for any help.
> Thanh
>
See DATEADD(dd, ... ) and DATEPART(dw, ...) in BOL, but first you will
need an unambiguous definition for fiscal period.|||Below is one method. I suggest you materialize these results in a fiscal
period table.
DECLARE @.first_of_year datetime
SET @.first_of_year = '20070101'
SELECT
DATEADD(day, (DATEPART(dw, DATEADD(month, month_number - 1,
@.first_of_year)) - 1) * -1,
DATEADD(month, month_number - 1, @.first_of_year)) AS PeriodStart,
DATEADD(day, (DATEPART(dw, DATEADD(month, month_number,
@.first_of_year))) * -1,
DATEADD(month, month_number, @.first_of_year)) AS PeriodEnd
FROM (SELECT 1 AS month_number UNION ALL SELECT 2 UNION ALL SELECT 3
UNION ALL SELECT 4 AS month_number UNION ALL SELECT 5 UNION ALL SELECT 6
UNION ALL SELECT 7 AS month_number UNION ALL SELECT 8 UNION ALL SELECT 9
UNION ALL SELECT 10 AS month_number UNION ALL SELECT 11 UNION ALL SELECT
12)
AS month_numbers
Hope this helps.
Dan Guzman
SQL Server MVP
"Chris.Cheney" <Chris.CheneyXXNOSPAMXX@.tesco.net> wrote in message
news:Xns9924696609362ChrisCheneytesconet
@.80.5.182.99...
> examnotes <ThanhNguyen@.discussions.microsoft.com>
> wrote in news:ABAF7E37-0534-42EB-95A6-23E95963D54A@.microsoft.com:
>
> See DATEADD(dd, ... ) and DATEPART(dw, ...) in BOL, but first you will
> need an unambiguous definition for fiscal period.|||Thank you very much, Dan.
"Dan Guzman" <guzmanda@.nospam-online.sbcglobal.net> wrote in message
news:3CFD7275-D58D-4D46-B5CB-DA1F3BBEC96A@.microsoft.com...
> Below is one method. I suggest you materialize these results in a fiscal
> period table.
> DECLARE @.first_of_year datetime
> SET @.first_of_year = '20070101'
> SELECT
> DATEADD(day, (DATEPART(dw, DATEADD(month, month_number - 1,
> @.first_of_year)) - 1) * -1,
> DATEADD(month, month_number - 1, @.first_of_year)) AS PeriodStart,
> DATEADD(day, (DATEPART(dw, DATEADD(month, month_number,
> @.first_of_year))) * -1,
> DATEADD(month, month_number, @.first_of_year)) AS PeriodEnd
> FROM (SELECT 1 AS month_number UNION ALL SELECT 2 UNION ALL SELECT 3
> UNION ALL SELECT 4 AS month_number UNION ALL SELECT 5 UNION ALL SELECT
> 6
> UNION ALL SELECT 7 AS month_number UNION ALL SELECT 8 UNION ALL SELECT
> 9
> UNION ALL SELECT 10 AS month_number UNION ALL SELECT 11 UNION ALL
> SELECT 12)
> AS month_numbers
>
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "Chris.Cheney" <Chris.CheneyXXNOSPAMXX@.tesco.net> wrote in message
> news:Xns9924696609362ChrisCheneytesconet
@.80.5.182.99...
>

No comments:

Post a Comment