SQLServerCentral.com / SQL Server 2008 / T-SQL (SS2K8) / Help with this Query<!-- 864 --> / Latest PostsInstantForum.NET v2.9.0SQLServerCentral.comhttp://www.sqlservercentral.com/Forums/notifications@sqlservercentral.comWed, 20 Aug 2014 08:53:01 GMT20RE: Help with this Queryhttp://www.sqlservercentral.com/Forums/Topic1360564-392-1.aspx[quote] best way to get the FiscalWeek based on the results from above? I don't want to hard code every single Calendar week to get the orresponding Fiscal Week. [/quote]The you are a lazy bum who is going to have a lot of trouble with databases. Ever hear the word "terabyte" or "petabyte" before? :-)A 100 year calendar table has only 36,525 days in it. You can generate it on a spreadsheet or word processor with macros. A useful idiom is a report period calendar. It gives a name to a range of dates.CREATE TABLE Report_Periods(report_name VARCHAR(30) NOT NULL PRIMARY KEY,report_start_date DATE DEFAULT CURRENT_TIMESTAMP NOT NULL,report_end_date DATE NOT NULL,CONSTRAINT date_orderingCHECK (report_start_date <= report_end_date),etc);These report periods can overlap; a fiscal quarter will be contained in the range of its fiscal year. There can be gaps between them; we have to wait a year between each “Annual Going out Of Business Sale!” and there might be long stretches of time without any special sales. But we want to know their ranges so that the table is fairly constant once it is created.I like the MySQL convention of using double zeroes for months and years, That is 'yyyy-mm-00' for a month within a year and 'yyyy-00-00' for the whole year. The advantage is that it will sort with the ISO-8601 data format required by Standard SQL. Since SQL is a database language, we prefer to do look ups and not calculations. I would build a calendar table with a ISO-8601 week-within-year format column in addition to the usual Common Era date. This format is 'yyyyWww-d' where yyyy is the year, W is as separator token, ww is (01-53) week number and d is (1-7) day of the week.You input any calendar date, find the week-within-year column and return the dates that match on a LIKE predicate.WHERE sale_day LIKE '2012W26-[67]'There are several websites with calendars you can cut & paste, but you can start your search with: http://www.calendar-365.com/week-number.html You can modify this for your fiscal calendar by replacing the 'W' with an 'F' for "Fiscal" and displacing the dates appropriately.Tue, 18 Sep 2012 16:59:32 GMTCELKORE: Help with this Queryhttp://www.sqlservercentral.com/Forums/Topic1360564-392-1.aspxWell theres a possible typo in your code for the DATEMNAME(wk,DateValue)=29 which is repeated twice. I dont know if this is by design or not, but it seems odd that you classify week 29 as Week 3 and week 30 as week 5. A Simpler solution would be to use this case statement ,CASE WHEN DATENAME(wk,DateValue)>=27 then DATENAME(wk,DateValue)-26 ELSE DATENAME(wk,DateValue)+27 endIt doesnt take into account any special logic like the WK 29 issue, and assumes every year follows the same pattern.It also makes the script easier to understand and maintain. As Robin suggested I would materialise this as a Calendar Table.Tue, 18 Sep 2012 02:15:59 GMTJason-299789RE: Help with this Queryhttp://www.sqlservercentral.com/Forums/Topic1360564-392-1.aspxFor this type of scenario, I would look at maybe implementing a calendar/tally table that holds all the necessary information against each day. My own calendar table stores (for each date) day number in year, fiscal month, fiscal year and other information relevant to the business. It's then just a simple case of linking the date in your transaction table to the calendar table to retrieve the columns required.We've expanded the calendar table recently to include such delights as: is a working day,is a national holiday, time sheet start day etc.There's lots of scripts around to generate these tables. SQLServerCentral.com and Google is your friend!Tue, 18 Sep 2012 01:12:27 GMTRobin SassonHelp with this Queryhttp://www.sqlservercentral.com/Forums/Topic1360564-392-1.aspxI have a date query as below:[code="sql"]SET DATEFIRST 1 -- Sets Monday as First Day of Week;with mycte as( select cast('2011-01-01' as datetime) DateValue union all select DateValue + 1 from mycte where DateValue + 1 < '2013-07-02')Select DateValue, DATENAME(dw,DateValue) As DayOfWeek, DATENAME(wk,DateValue) As CalendarWeek, Case When DATENAME(wk,DateValue) = 27 And Year(DateValue) = 2012 Then 2013 Else 2012 End As FinancialYear, Case When DATENAME(wk,DateValue) = 27 And Year(DateValue) = 2011 Then 1 When DATENAME(wk,DateValue) = 28 And Year(DateValue) = 2011 Then 2 When DATENAME(wk,DateValue) = 29 And Year(DateValue) = 2011 Then 3 When DATENAME(wk,DateValue) = 29 And Year(DateValue) = 2011 Then 4 When DATENAME(wk,DateValue) = 30 And Year(DateValue) = 2011 Then 5 When DATENAME(wk,DateValue) = 31 And Year(DateValue) = 2011 Then 6 When DATENAME(wk,DateValue) = 32 And Year(DateValue) = 2011 Then 7 When DATENAME(wk,DateValue) = 33 And Year(DateValue) = 2011 Then 8 When DATENAME(wk,DateValue) = 34 And Year(DateValue) = 2011 Then 9 When DATENAME(wk,DateValue) = 35 And Year(DateValue) = 2011 Then 10 When DATENAME(wk,DateValue) = 36 And Year(DateValue) = 2011 Then 11 When DATENAME(wk,DateValue) = 37 And Year(DateValue) = 2011 Then 12 When DATENAME(wk,DateValue) = 38 And Year(DateValue) = 2011 Then 13 When DATENAME(wk,DateValue) = 39 And Year(DateValue) = 2011 Then 14 When DATENAME(wk,DateValue) = 40 And Year(DateValue) = 2011 Then 15 When DATENAME(wk,DateValue) = 41 And Year(DateValue) = 2011 Then 16 When DATENAME(wk,DateValue) = 42 And Year(DateValue) = 2011 Then 17 When DATENAME(wk,DateValue) = 43 And Year(DateValue) = 2011 Then 18 When DATENAME(wk,DateValue) = 44 And Year(DateValue) = 2011 Then 19 When DATENAME(wk,DateValue) = 45 And Year(DateValue) = 2011 Then 20 When DATENAME(wk,DateValue) = 46 And Year(DateValue) = 2011 Then 21 When DATENAME(wk,DateValue) = 47 And Year(DateValue) = 2011 Then 22 When DATENAME(wk,DateValue) = 48 And Year(DateValue) = 2011 Then 23 When DATENAME(wk,DateValue) = 49 And Year(DateValue) = 2011 Then 24 When DATENAME(wk,DateValue) = 50 And Year(DateValue) = 2011 Then 25 When DATENAME(wk,DateValue) = 51 And Year(DateValue) = 2011 Then 26 When DATENAME(wk,DateValue) = 52 And Year(DateValue) = 2011 Then 27 When DATENAME(wk,DateValue) = 53 And Year(DateValue) = 2011 Then 28 When DATENAME(wk,DateValue) = 1 And Year(DateValue) = 2012 Then 28 When DATENAME(wk,DateValue) = 2 And Year(DateValue) = 2012 Then 29 When DATENAME(wk,DateValue) = 3 And Year(DateValue) = 2012 Then 30 When DATENAME(wk,DateValue) = 4 And Year(DateValue) = 2012 Then 31 When DATENAME(wk,DateValue) = 5 And Year(DateValue) = 2012 Then 32 When DATENAME(wk,DateValue) = 6 And Year(DateValue) = 2012 Then 33 When DATENAME(wk,DateValue) = 7 And Year(DateValue) = 2012 Then 34 When DATENAME(wk,DateValue) = 8 And Year(DateValue) = 2012 Then 35 When DATENAME(wk,DateValue) = 9 And Year(DateValue) = 2012 Then 36 When DATENAME(wk,DateValue) = 10 And Year(DateValue) = 2012 Then 37 When DATENAME(wk,DateValue) = 11 And Year(DateValue) = 2012 Then 38 When DATENAME(wk,DateValue) = 12 And Year(DateValue) = 2012 Then 39 When DATENAME(wk,DateValue) = 13 And Year(DateValue) = 2012 Then 40 When DATENAME(wk,DateValue) = 14 And Year(DateValue) = 2012 Then 41 When DATENAME(wk,DateValue) = 15 And Year(DateValue) = 2012 Then 42 When DATENAME(wk,DateValue) = 16 And Year(DateValue) = 2012 Then 43 When DATENAME(wk,DateValue) = 17 And Year(DateValue) = 2012 Then 44 When DATENAME(wk,DateValue) = 18 And Year(DateValue) = 2012 Then 45 When DATENAME(wk,DateValue) = 19 And Year(DateValue) = 2012 Then 46 When DATENAME(wk,DateValue) = 20 And Year(DateValue) = 2012 Then 47 When DATENAME(wk,DateValue) = 21 And Year(DateValue) = 2012 Then 48 When DATENAME(wk,DateValue) = 22 And Year(DateValue) = 2012 Then 49 When DATENAME(wk,DateValue) = 23 And Year(DateValue) = 2012 Then 50 When DATENAME(wk,DateValue) = 24 And Year(DateValue) = 2012 Then 51 When DATENAME(wk,DateValue) = 25 And Year(DateValue) = 2012 Then 52 When DATENAME(wk,DateValue) = 26 And Year(DateValue) = 2012 Then 53 When DATENAME(wk,DateValue) = 27 And Year(DateValue) = 2012 Then 1 End As FiscalWeek -- this is only an exampleFrom myctewhere DateValue between '2011-06-27' and '2012-07-01'OPTION (MAXRECURSION 0)[/code]What is the best way to get the FiscalWeek based on the results from above? I don't want to hard code every single Calendar week to get the corresponding Fiscal Week. Financial Year starts on 01/JulyMon, 17 Sep 2012 23:34:04 GMTrka