September 24, 2008 at 8:34 am
Hello
Am using the following query to determine a fiscal week. The result output is fine and i get '200843'; but i will like to format the date output to '2008-43', does anyone know how?
drop function FiscalWeek
go
create function FiscalWeek (@startMonth varchar(2), @myDate datetime)
returns int
as
begin
declare @firstWeek datetime
declare @weekNum int
declare @year int
set @year = datepart(year, @myDate)+1
--Get 4th day of month of next year, this will always be in week 1
set @firstWeek = convert(datetime, str(@year)+@startMonth+'04', 102)
--Retreat to beginning of week
set @firstWeek = dateadd(day, (-datepart(dw, @firstWeek)), @firstWeek)
while @myDate < @firstWeek --Repeat the above steps but for previous year
begin
set @year = @year - 1
set @firstWeek = convert(datetime, str(@year)+@startMonth+'04', 102)
set @firstWeek = dateadd(day, (1-datepart(dw, @firstWeek)), @firstWeek)
end
set @weekNum = (@year*100)+((datediff(day, @firstweek, @myDate)/7)+2)
return @weekNum
end
September 24, 2008 at 8:42 am
You are using all numeric types to you won't get the result you want as 2008-43 is a character type, not a numeric.
I'd change @WeekNum to char(7) and then when setting @WeekNum I'd do this:
set @weekNum = Convert(Char(4), (@year*100)) + '-' + Convert(char(2), ((datediff(day, @firstweek, @myDate)/7)+2))
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
September 24, 2008 at 8:49 am
You will need to cast your return value as CHAR (or VARCHAR) if you want an embedded non-numeric character:
[font="Courier New"]DECLARE @weekNum INT, @WeekName CHAR(7)
SET @weekNum = 200843
SET @WeekName = STUFF(CAST(@weekNum AS VARCHAR(7)), 5, 0, '-')
SELECT @weekNum, @WeekName
[/font]
Cheers
ChrisM
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
September 24, 2008 at 8:52 am
Hello Jack
Got the following error msg:
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value '*-43' to data type int.
September 24, 2008 at 9:01 am
Sorry, I was trying to figure out why you had the * 100 and now I know. You need to remove the *100. The code should be:
set @weekNum = Convert(Char(4), @year) + '-' + Convert(char(2), ((datediff(day, @firstweek, @myDate)/7)+2))/code]
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
September 24, 2008 at 9:14 am
Hello Jack
the year and week together (multiply year by 100 to make room for the week number and add it on) We include the year so that if the result set is more than a year in scope say 2004 - 2006 , week 5 for 2004 won't get grouped with week 5 in 2005 and 2006. And the week number is easily separated using modulus (example 200405 % 100 = 5 )
September 27, 2008 at 7:49 pm
Stuff it...:P seriously... the STUFF solution that Chris Morris posted will do the trick just fine.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply