[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL - Between Dates with Dynamic Year from Parameter

Posted on 2011-03-24
6
Medium Priority
?
803 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
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Suggested Courses

834 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