Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Best practice on "CASE" script

Posted on 2014-03-31
2
208 Views
Last Modified: 2014-03-31
The script i have works but very long and i have run into a "CASE" statement limit so i can not go as far back in history as i would like.  Here is a example of what i am currently using.
SELECT ITEM_NO, 
	CASE 
		WHEN datediff(DD, [BUS_DAT], GETDATE()) <= 7 THEN 'Week_01' 
		WHEN datediff(DD, [BUS_DAT], GETDATE()) <= 14 THEN 'Week_02' 
		WHEN datediff(DD, [BUS_DAT], GETDATE()) <= 21 THEN 'Week_03' 
		WHEN datediff(DD, [BUS_DAT], GETDATE()) <= 28 THEN 'Week_04' 
		WHEN datediff(DD, [BUS_DAT], GETDATE()) <= 35 THEN 'Week_05'
		WHEN datediff(DD, [BUS_DAT], GETDATE()) <= 42 THEN 'Week_06'
		WHEN datediff(DD, [BUS_DAT], GETDATE()) <= 49 THEN 'Week_07' 
                WHEN datediff(DD, [BUS_DAT], GETDATE()) <= 56 THEN 'Week_08' 
                WHEN datediff(DD, [BUS_DAT], GETDATE()) <= 63 THEN 'Week_09' 
                WHEN datediff(DD, [BUS_DAT], GETDATE()) <= 70 THEN 'Week_10' 
		ELSE NULL 
	END AS SALE_WEEK, QTY_SOLD
FROM dbo.PS_TKT_HIST_LIN

Open in new window


What this is doing is labeling each line according to what week the sale falls in to by counting back 7 day from the current date to create week one and so on and so forth.  I'm looking to a way to not use so many when clauses and i can look back 3 or 4 years if i want.

Thank you for your help and let me know if you have any further questions.
0
Comment
Question by:Littlet5621
2 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 500 total points
ID: 39967651
SELECT ITEM_NO,
   'Week_' + RIGHT('0' + CAST(DATEDIFF(DAY, BUS_DAT, GETDATE()) / 7 + 1 AS VARCHAR), 2),
   ...
0
 
LVL 35

Expert Comment

by:David Todd
ID: 39967790
Hi,

select dateadd( week, (datediff( week, 0, getdate()), 0 ) should give the date at the start of the week.

You may need to change the 0 to -1 or something as the base datetime. Depends on what you want as the starting day for the week.

HTH
  David
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question