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

x
?
Solved

Sending Greater Than and Less than with Date Parameters

Posted on 2007-11-27
3
Medium Priority
?
2,552 Views
Last Modified: 2008-02-01
Hi all,
 I am trying to construct a parametersed query. I have constructed a stored prodedure and an asp.net class which seems to be doing everything i need. the only last problem i have is with the dates. This is the stored procedure:

ALTER PROCEDURE [dbo].[SelectApplications] (@titleFilter  varchar(50),@appType varchar(3), @programme varchar(3),@status varchar(3), @startDate varchar(15),@endDate varchar(15),@department varchar(30))
AS
declare @sql varchar(2000)
BEGIN
set @sql = 'select top 100 * from applications where
app_type like ''' + @appType  + ''' and app_title1 like ''%' + @titleFilter + '%'' and
progid like  ''' + @programme + ''' and
app_status like ''' + @status + ''' and
startDate ' + @startDate + ' + and
endDate + ' + @endDate + ' +  and
DeptId like ''' + @department + ''' '
exec (@sql)
END

My asp.net class sends the following code to the stored procedure:
exec SelectApplicationsView @titleFilter='',@appType='%',@programme='%',@Status='%',@startDate='< 1/1/2000',@endDate='> 1/1/1900',@department='%'

as you can hopefully see, my problem is that the dates are being put in single quotes when in actual fact they shouldn't be in single quotes.

do you know how to acheive this in dynamic sql?
or should i be doing this query another way?

thanks!
crich


0
Comment
Question by:crich
3 Comments
 
LVL 16

Expert Comment

by:SQL_SERVER_DBA
ID: 20360804
use BETWEEN
0
 
LVL 18

Accepted Solution

by:
Yveau earned 800 total points
ID: 20362235
Change it to:

Hope this helps ...
ALTER   PROCEDURE [dbo].[SelectApplications] 
(       @titleFilter  varchar(50)
,       @appType varchar(3)
,       @programme varchar(3)
,       @status varchar(3)
,       @startDate varchar(15)
,       @endDate varchar(15)
,       @department varchar(30))
AS
declare @sql varchar(2000)
BEGIN
		set @sql = 'select top 100 * from applications where app_type like ''' 
		+ @appType  + ''' and app_title1 like ''%' 
		+ @titleFilter + '%'' and progid like  ''' 
		+ @programme + ''' and app_status like ''' 
		+ @status + ''' and startDate ' 
		+ @startDate + ' and endDate ' 
		+ @endDate + ' and DeptId like ''' 
		+ @department + ''' ' 
		exec (@sql)
END
 
 
--

Open in new window

0
 

Author Comment

by:crich
ID: 20364586
hi thanks for the replies. i will have to give points to Yveau as the solution built upon my code even though i expect i could have tried between. i hope this is ok...

Thanks a lot for your help, it worked fine! I had to add some convert statements too..

richard
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Loops Section Overview
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

916 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