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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select Statement

Author  Topic 

Vack
Aged Yak Warrior

530 Posts

Posted - 2012-02-07 : 12:18:08
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 Period

How would I get that logic into a select statement

select month, ???? as period
from 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
Go to Top of Page

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))
GO

INSERT INTO Fiscal_Period (Month_num, Period_num, Month_Name)
SELECT 1, 7, 'January' UNION ALL
SELECT 2, 8, 'February' UNION ALL
ect

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -