Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Best practice on "CASE" script

Posted on 2014-03-31
2
Medium Priority
?
230 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
[X]
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
2 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

618 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