Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL how to format result set IF variable date is >= or <=

Posted on 2008-06-15
6
Medium Priority
?
900 Views
Last Modified: 2008-11-02
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.
0
Comment
Question by:digital_slavery
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
frankytee earned 1000 total points
ID: 21788366
use the YEAR function
select right('0' + right(cast(year(yourdatefield) as varchar(4)),2),2) as yy,
....etc
from yourtable
0
 
LVL 19

Expert Comment

by:frankytee
ID: 21788444
didn't read your question properly before, try:

select
  case
    when month(yourdatefield) between 10 and 12 then      --next year
        right('0' + right(cast((year(yourdatefield) + 1) as varchar(4)),2),2)
    else  --this year
       right('0' + right(cast((year(yourdatefield)) as varchar(4)),2),2)
  end as FY
.... etc
from yourtable
0
 
LVL 51

Assisted Solution

by:Mark Wills
Mark Wills earned 1000 total points
ID: 21788640
Interesting, can also be readily handled in most report writers as well...

I have simply used getdate()  as an example - you should replace with the appropriate columns etc etc...

select right(year(getdate()),2) as current_YY, right(year(dateadd(mm,3,getdate())),2) as current_fy

do you also want the WHERE clause to calculate start of financial year up to "yesterday" ?  This time we will convert to datetime so it can be used directly in a where

select convert(datetime,'01 Oct '+convert(varchar,year(dateadd(mm,3,getdate()-1)))) as Start_of_FY,
       convert(datetime,'30 Sep '+convert(varchar,year(dateadd(mm,3,getdate()-1)))) as End_of_FY,
       convert(datetime,convert(varchar, getdate() -1,106)+' 23:59:59.9') as yesterday
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

877 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