Solved

Best practice on "CASE" script

Posted on 2014-03-31
2
205 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
Comment Utility
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
Comment Utility
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now