SQL WHERE CLAUSE GRIDVIEW FILTER

BOEING39
BOEING39 used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
PortletPaulEE Topic Advisor
Most Valuable Expert 2014
Awarded 2013

Commented:
>= 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.

Author

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

EE Topic Advisor
Most Valuable Expert 2014
Awarded 2013
Commented:
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

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

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

Again thx for everything.

Author

Commented:
Quick accurate response with great detail
PortletPaulEE Topic Advisor
Most Valuable Expert 2014
Awarded 2013

Commented:
Thanks! it's nice to know one's efforts are appreciated. Cheers, Paul

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial