Solved

SQL - Between Dates with Dynamic Year from Parameter

Posted on 2011-03-24
6
750 Views
Last Modified: 2012-05-11
I have a query that I am using to pull hours for specific employees based on Fiscal Year.  The month and day of the Fiscal year is static but the year of course changes.

This year the Fiscal year runs from 07/01/2010 to 06/30/2011. (2011 Fiscal Year)
There is one parameter for the Fiscal Year: @FiscYear
I would like to let the user enter the Fiscal Year and have it pull the data between the two dates above, if they enter 2011.  If they would enter 2012 the dates would dynmically become 07/01/2011 to 06/30/2012.

I know this syntax is incorrect, but I think you can get my idea from it:
 
SELECT     WempID, Windicator, Whours, Wdate, CASE WIP.Wbillable WHEN '1' THEN WIP.Whours ELSE 0 END AS 'BillActual'
FROM         WIP
WHERE  (Wdate BETWEEN convert(datetime,'07/01/' + (@FiscYear -1),101) AND convert(datetime, '06/30/' + @FiscYear,101)) AND
                      (WempID = @MyID) AND (Windicator <> 'D')

Open in new window


Error Recieved: Conversion failed when converting the varchar value '07/01/' to data type int.
Thanks!
0
Comment
Question by:ITMcmcpa
6 Comments
 
LVL 9

Expert Comment

by:Roman Gherman
Comment Utility
SELECT     WempID, Windicator, Whours, Wdate, CASE WIP.Wbillable WHEN '1' THEN WIP.Whours ELSE 0 END AS 'BillActual'
FROM         WIP
WHERE  (Wdate BETWEEN convert(datetime,'07/01/' + CONVERT(VARCHAR,(@FiscYear -1)),101) AND convert(datetime, '06/30/' + CONVERT(VARCHAR, @FiscYear),101)) AND
                      (WempID = @MyID) AND (Windicator <> 'D')
0
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 500 total points
Comment Utility
Try using dateadd instead:

SELECT     WempID, Windicator, Whours, Wdate, CASE WIP.Wbillable WHEN '1' THEN WIP.Whours ELSE 0 END AS 'BillActual'
FROM         WIP
WHERE  (Wdate BETWEEN DATEADD(yy, @FiscYear - 2001, '07/01/2000') AND DATEADD(yy, @FiscYear - 2000, '06/30/2000')
                      (WempID = @MyID) AND (Windicator <> 'D')
0
 

Author Closing Comment

by:ITMcmcpa
Comment Utility
Works perfect.  Thank you very much!
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 39

Expert Comment

by:lcohan
Comment Utility
For performance considerations as well do not use cast/convert or other functions in WHERE clause as they invalidate existing indexes and make sure you match the table.column datatype

declare @FiscYear sysname
set @FiscYear = 2012
IF @FiscYear = (select(year(getdate()))) set @FiscYear = 2010;
IF @FiscYear = (select(year(getdate()))+1) set @FiscYear = 2011;
print @FiscYear

declare @startdate datetime
declare @enddate sysname

set @startdate = (select convert(datetime,'07/01/' + @FiscYear,101))
set @enddate = (select convert(datetime, '06/30/' + @FiscYear,101))

SELECT     WempID, Windicator, Whours, Wdate, CASE WIP.Wbillable WHEN '1' THEN WIP.Whours ELSE 0 END AS 'BillActual'
FROM         WIP
WHERE  Wdate BETWEEN @startdate AND @enddate AND WempID = @Myid AND Windicator <> 'D'

0
 

Author Comment

by:ITMcmcpa
Comment Utility
roma1123 yours worked too.  Sorry I didn't see your post at first for some reason.
0
 
LVL 9

Expert Comment

by:Roman Gherman
Comment Utility
:)

No worries
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

743 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