views:

65

answers:

3

Please see attached image

alt text

I have a table which have FromDate and ToDate. FromDate is start of some event and ToDate is end of that event. I need to find a record if search criteria is in between range of dates.

e.g.

If a record has FromDate 2010/15/5 and ToDate 2010/15/25 and my criteria is FromDate 2010/5/18 and ToDate is 2010/5/21 then this record should be in search results because this is in the range of 15 to 25.

Following is my search query (chunk of)

SELECT   m.EventId
FROM     MajorEvents 

WHERE   (   (m.LocationID = @locationID OR @locationID IS NULL) OR M.LocationID IS NULL)
AND      (
            CONVERT(VARCHAR(10),M.EventDateFrom,23) BETWEEN  CONVERT(VARCHAR(10),@DateTimeFrom,23) AND CONVERT(VARCHAR(10),@DateTimeTo,23) 
            OR
            CONVERT(VARCHAR(10),M.EventDateTo,23) BETWEEN CONVERT(VARCHAR(10),@DateTimeFrom,23) AND CONVERT(VARCHAR(10),@DateTimeTo,23)
        )

If Search Criteria is equal to FromDate or ToDate then results are ok e.g. If search criterai is DateFrom = 2010/5/15 AND DateTo = 2010/5/18 then this record will return becasue Date From is exactly what is DateFrom in db.

OR

If search criteria is DateFrom = 2010/5/22 AND DateTo = 2010/5/25 then this record will return because Date To is exactly what is DateTo in db

But if anything in between this range it does not work

Thanks for the help.

Edit:

I can not use <= or > because this will bring all other records which are less than and greater than search criteria dates.

I just want to fetch those records which are on those dates E.g. FromDate = 2010/5/15 and DateTo = 2010/5/25 This is date range but event is on all dates in between so one solution is that I store all dates from 2010/5/15 to 2010/5/25 in separate table but if I can do this using query?

You can also tell me that it is not possible.

+1  A: 

Why don't you use an ISO format when converting to strings, having said that, your query will perform very badly because it is not SARGable, please read How Does Between Work With Dates In SQL Server? it will show you that you have to use where EventDate >= ... AND EventDate < ....

SQLMenace
Thanks for the help. Please see my Edit in Question.
Muhammad Kashif Nadeem
between is shorthand for >= and <= so between 1 and 3 is the same as >= 1 and <= 3
SQLMenace
+1  A: 

What about this:

SELECT   m.EventId
FROM     MajorEvents AS m
WHERE   (   
                ((m.LocationID = @locationID) OR (@locationID IS NULL))
            OR  (m.LocationID IS NULL)
        )
    AND (
                (DATEADD(DAY, DATEDIFF(DAY, 0, m.EventDateFrom), 0) <= DATEADD(DAY, DATEDIFF(DAY, 0, m.@DateTimeTo), 0))
            AND (DATEADD(DAY, DATEDIFF(DAY, 0, m.EventDateTo), 0) >= DATEADD(DAY, DATEDIFF(DAY, 0, m.@DateTimeFrom), 0))
        )
NYSystemsAnalyst
This worked but can you please tell me what is the reason to use DATEADD AND DATEDIFF functions with 0 because 0 will not effect on date. Thanks for the help.
Muhammad Kashif Nadeem
I noticed in the image you posted that the dates had times associated with them. The DATEADD and DATEDIFF functions, when used as shown, set the time value to midnight for all dates. In effect, it removes the time as a factor. Alternatively, you could add 1 day to @DateTimeTo and change the comparison to <. If you don't take this precaution with the times, you may end up leaving out records because the time value is greater than midnight. I hope I explained this clearly enough. It is just another solution to the problem discussed in the link posted by SQLMenace.
NYSystemsAnalyst
Thanks, great help.
Muhammad Kashif Nadeem
A: 

To second NTSystemAnalyst's post, try this option:

SELECT m.EventId FROM MajorEvents AS m WHERE (
((m.LocationID = @locationID) OR (@locationID IS NULL)) OR (m.LocationID IS NULL) )

AND ( m.EventDateFrom < DATEADD(dd, 1, CONVERT(varchar(10), @DateTimeTo, 23)) AND m.EventDateTo > DATEADD(dd, -1, CONVERT(varchar(10), @DateTimeFrom, 23)) )

Reagan Williams