digital_slavery
asked on
SQL how to format result set IF variable date is >= or <=
I am writing a report that needs to display a column based off a date column. In my example I need the query to take the date that is already in the column and display an identifier based off which year it is. My query uses 2 dates as parameters to get a result set.
for example:
Today is June 15th 2008, so when the query is ran today and the startDate param is Sept 1 2007 and the end date is June 14th 2008 the query would need to analyze the results set to format the column to display either 07 for any items that have 2007 and 2008 for any thing that has 2008 ect ect. I would acutally love it if I could automate this so it does not matter what year the query could just keep populating the column as each year passes. So next year the column would automatically out put 09 for the FY column.
Also the FY is from Oct 1st to Sept 31 which the query needs to use when determining how to format the results.
for example:
Today is June 15th 2008, so when the query is ran today and the startDate param is Sept 1 2007 and the end date is June 14th 2008 the query would need to analyze the results set to format the column to display either 07 for any items that have 2007 and 2008 for any thing that has 2008 ect ect. I would acutally love it if I could automate this so it does not matter what year the query could just keep populating the column as each year passes. So next year the column would automatically out put 09 for the FY column.
Also the FY is from Oct 1st to Sept 31 which the query needs to use when determining how to format the results.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
select
case
when month(yourdatefield) between 10 and 12 then --next year
right('0' + right(cast((year(yourdatef
else --this year
right('0' + right(cast((year(yourdatef
end as FY
.... etc
from yourtable