Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
I have a fiscal year that starts in July 1. I have a field that has the month so I want a select statement that will also include the Period. So July is period 1 and June is Period 12. I'm trying for something like if month <=6 then month + 6 else month - 6 as PeriodHow would I get that logic into a select statementselect month, ???? as periodfrom table
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts
Posted - 2012-02-07 : 12:22:49
You can use a case expression:
SELECT CASE WHEN month <=6 then month + 6 else month - 6 END as Period
Or even
SELECT (month+7)%12 AS Period
X002548
Not Just a Number
15586 Posts
Posted - 2012-02-07 : 12:32:57
When you say "field that has the month". How is that stored, as numbers or letters?It sounds specific to your application, so I would create a table with your values and join to it..let's assume your month is numeric.CREATE TABLE Fiscal_Period (Month_num int, Period_num int, Month_Name varchar(20))GOINSERT INTO Fiscal_Period (Month_num, Period_num, Month_Name)SELECT 1, 7, 'January' UNION ALLSELECT 2, 8, 'February' UNION ALLectBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/