SQL WHERE CLAUSE GRIDVIEW FILTER

The following select code is utilized to filter a gridview date range search.   It is working; however, if I perform a singe date range search for example:   9/16/13 through 9/16/13 it will not return the 9/16/13 data.  If I open up the search one day to 9/17/13 it will display the 9/16/13 data and not the 9/17/13 data and so on.   I have tried using  >= and <= but getting the same result as the between function.




SelectCommand="SELECT [ID], [Dates], [Ship], [Station], [Otbd], [Dep], [Code], [Time], [Code1], [Time1], [Wake], [Call], [MM], [RB], [Reason], [ATA], [RON], [GT], [Captain] FROM [Delays] WHERE [Dates] between @Dates and @Dates2 AND [Dep] between @Dep and @Dep2 AND [Station] = @Station AND [Code] IN ('MC', 'MM', 'MA', 'MF', 'MH', 'ML', 'MN') ORDER BY [Dates] DESC" 

Open in new window

BOEING39Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
>= and <=
is exactly what 'between' does (it actually gets translated into this when executed)

the most reliable method for date range filtering is

>= and <

for more on this topic please see: "Beware of Between"

{+edit}

like this:

declare @Dates as datetime, @Dates2 as datetime

set @Dates  = '20130916'
set @Dates2 = '20130917' -- is "the next day"

...
WHERE ( [Dates] >= @Dates AND [Dates] < @Dates2 )

-- expect data for 9/16/2013
-----------------------------------------
declare @Dates as datetime, @Dates2 as datetime

set @Dates  = '20130916'
set @Dates2 = '20130918' -- is "the next day"

...
WHERE ( [Dates] >= @Dates AND [Dates] < @Dates2 )

-- expect data for 9/16/2013 & 9/17/2013

Note, this may mean you MIGHT have to add 1 day to @dates2
e.g. if accepting a date selection from a web page, a use might select
9/16/2013 (@dates)
and
9/16/2013 (@dates2)

(i.e. they just want 1 day's data)

you may therefore want to use dateadd(day,1,@Dates2)

WHERE ( [Dates] >= @Dates AND [Dates] < dateadd(day,1,@Dates2) )

If this is as clear as mud, let me know please.
0
BOEING39Author Commented:
I am getting the following error with the code below:

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near ')'.



SelectCommand="SELECT [ID], [Dates], [Ship], [Station], [Otbd], [Dep], [Code], [Time], [Code1], [Time1], [Wake], [Call], [MM], [RB], [Reason], [ATA], [RON], [GT], [Captain] FROM [Delays] WHERE ([Dates] >= @Dates) and [Dates] < dateadd(day,1,@Dates2)) AND [Dep] between @Dep and @Dep2 AND [Station] = @Station AND [Code] IN ('MC', 'MM', 'MA', 'MF', 'MH', 'ML', 'MN') ORDER BY [Dates] DESC"

Open in new window

0
PortletPaulfreelancerCommented:
one too many ending parenthesis immediately after @Dates in the where clause, try this
SelectCommand="SELECT [ID], [Dates], [Ship], [Station], [Otbd], [Dep], [Code], [Time], [Code1], [Time1], [Wake], [Call], [MM], [RB], [Reason], [ATA], [RON], [GT], [Captain] FROM [Delays] WHERE ([Dates] >= @Dates and [Dates] < dateadd(day,1,@Dates2)) AND [Dep] between @Dep and @Dep2 AND [Station] = @Station AND [Code] IN ('MC', 'MM', 'MA', 'MF', 'MH', 'ML', 'MN') ORDER BY [Dates] DESC"

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

BOEING39Author Commented:
That did it.   Thx Paul.   I will look at the other options you sent me tomorrow.  

Again thx for everything.
0
BOEING39Author Commented:
Quick accurate response with great detail
0
PortletPaulfreelancerCommented:
Thanks! it's nice to know one's efforts are appreciated. Cheers, Paul
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.