?
Solved

SQL - Between Dates with Dynamic Year from Parameter

Posted on 2011-03-24
6
Medium Priority
?
799 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 9

Expert Comment

by:Roman Gherman
ID: 35209562
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 23

Accepted Solution

by:
Snarf0001 earned 2000 total points
ID: 35209589
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
ID: 35209690
Works perfect.  Thank you very much!
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 40

Expert Comment

by:lcohan
ID: 35209697
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
ID: 35209715
roma1123 yours worked too.  Sorry I didn't see your post at first for some reason.
0
 
LVL 9

Expert Comment

by:Roman Gherman
ID: 35209771
:)

No worries
0

Featured Post

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

777 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