suppose the fiscal year is starting from July/1st to June/30th.
I have to calculate the week no. accordingly in SQL Server 2005.
Please Suggest!
Many Tha
If week 1 of your financial year is always 1st July to 7th July...
DECLARE
@inputDate DATETIME,
@fYearStart DATETIME,
@weekNumber INT
SET
@inputDate = getDate()
SET
@fYearStart = DATEADD(year, DATEDIFF(year, '20000101', DATEADD(month, -7, @inputDate)), '20000701')
SET
@weekNumber = DATEDIFF(day, @fYearStart, @inputDate) / 7 + 1
If your finacial weeks are always Sunday to Saturday...
DECLARE
@inputDate DATETIME,
@fYearStart DATETIME,
@weekNumber INT
SET
@inputDate = getDate()
SET
@fYearStart = DATEADD(year, DATEDIFF(year, '20000101', DATEADD(month, -7, @inputDate)), '20000701')
SET
@weekNumber = DATEDIFF(WEEK, @fYearStart, @inputDate)
One or other of these should be adaptable to your definition of week number
.
This one worked for me:
SELECT dt AS DayDate,
CONVERT(VARCHAR,DATENAME(weekday,dt)) AS [DayOfWk],
CONVERT(int,CONVERT(VARCHAR,DATEPART(yyyy,dt)) + RIGHT('00' + CONVERT(VARCHAR,DATEPART(wk,dt)),2)) AS [WeekID],
'Week ' + CONVERT(VARCHAR,DATEPART(wk,dt))AS [WeekDsc],
DATEPART(wk,dt) AS [WeekNbr],
DATEADD(dd, -(DATEPART(dw, dt)-1), dt) AS [WeekStartDay],
DATEADD(dd, 7-(DATEPART(dw, dt)), dt) AS [WeekEndDay],
CONVERT(VARCHAR,CASE WHEN DATEPART(mm,dt) > 6 THEN DATEPART(yyyy,dt) + 1 ELSE DATEPART(yyyy,dt) END) + RIGHT('00'+ CONVERT(VARCHAR,DATEDIFF(week,DATEADD(dd, 7-(DATEPART(dw, CASE WHEN DATEPART(mm,dt) > 6 THEN CONVERT(VARCHAR,DATEPART(yyyy,dt)) + '-07-01' ELSE CONVERT(VARCHAR,DATEPART(yyyy,dt) - 1) + '-07-01' END)), CASE WHEN DATEPART(mm,dt) > 6 THEN CONVERT(VARCHAR,DATEPART(yyyy,dt)) + '-07-01' ELSE CONVERT(VARCHAR,DATEPART(yyyy,dt) - 1) + '-07-01' END) + 1,DATEADD(dd, -(DATEPART(dw, dt)-1), dt)) + 2) ,2) AS [FinWkID]
FROM
(
SELECT DATEADD(day,increment,'2014-01-01') dt
FROM
(
SELECT top 5000 ROW_NUMBER() OVER (ORDER BY s1.[object_id]) - 1 AS increment
FROM sys.all_objects AS s1
CROSS JOIN sys.all_objects AS s2
ORDER BY s1.[object_id]
) SQ
) DATE_GENERATOR
try this
select DATEPART(week,getdate())-DATEPART(week,'07-01-2012')
To get the week number starting from July 1st 2012
Try this and use variable @dt for your own needs:
DECLARE @dt DATETIME = GETDATE()
SELECT WeekOfMonth = DATEPART(wk, @dt) - DATEPART(wk,DATEADD(m, DATEDIFF(M, 0, @dt), 0)) + 1
EDITED: My fault as I incorrectly understood the question, my solution returns the week of the month, not of the year.
Using part of @Dems answer and changing mine here is a full working test that outputs 3 columns WeekOfMonth, WeekOfYear and WeekOfFIscalYear based on date and begining of fiscal year available on a temporary table. But I guess the begining of a fiscal year will be always the same for a particular company. I just added different dates and years for testing.
DECLARE @TT TABLE (auxVal INT,
auxdate DATETIME,
fiscal_year DATETIME
)
INSERT @TT
SELECT 100,'19120101 00:00:00','19120701' UNION ALL
SELECT 200,'18120615 00:00:00','18110701' UNION ALL
SELECT 100,'20121121 00:00:00','20120701' UNION ALL
SELECT 200,'20120101 00:00:00','20110701' UNION ALL
SELECT 100,'20150802 00:00:00','20140701' UNION ALL
SELECT 200,'20120330 00:00:00','20110701' UNION ALL
SELECT 322,'20110228 00:00:00','20100701'
SELECT DATEDIFF(week, DATEADD(MONTH, DATEDIFF(MONTH, 0, auxDate), 0), auxDate) + 1 WeekOfMonth,
DATEPART( wk, auxDate) WeekOfYear,
DATEDIFF(DAY, (DATEADD(YEAR, DATEDIFF(YEAR, fiscal_year, DATEADD(MONTH, -7, auxDate)), fiscal_year)), auxDate) / 7 + 1 WeekOfFiscalYear
FROM @TT
Result:
WeekOfMonth WeekOfYear WeekOfFiscalYear
-------------------------------------------
1 1 27
3 25 51
4 47 21
1 1 27
2 32 5
5 13 40
5 10 35
-------------------------------------------
Without the need of creating a table. Just replace the @date with your column date name
declare @date datetime
set @date = '12/8/2016 00:00:00'
select case when datepart(month,@date) > 6 then
case when CEILING(datediff(day,'6/30/' + cast(datepart(year,@date) as nvarchar(4)),@date) / 7.0) > 52
then 52 else CEILING(datediff(day,'6/30/' + cast(datepart(year,@date) as nvarchar(4)),@date) / 7.0) end
else
case when CEILING(datediff(day,'6/30/' + cast(datepart(year,@date)-1 as nvarchar(4)),@date) / 7.0) > 52
then 52 else CEILING(datediff(day,'6/30/' + cast(datepart(year,@date)-1 as nvarchar(4)),@date) / 7.0) end
end WorkWeek
Compute Week number based on Fiscal Year