Solved

SQL - Between Dates with Dynamic Year from Parameter

Posted on 2011-03-24
6
771 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 22

Accepted Solution

by:
Snarf0001 earned 500 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 39

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

810 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