posts - 50, comments - 168, trackbacks - 6

My Links

News



View Marcin Celej's profile on LinkedIn

Archives

Post Categories

SQL date filtering - between date from and to

I ve been recently creating few reports that required dates as parameters. It is quite common to have a report that must be provided range of dates (from and to). I investigated few reports that was created by my colleagues and I found those reports are not intuitive. Let me explain:

When I see two dates that must be provided to a report I believe that when I provide the same date in from and to parameters it will show all data for the paricular day. See screenshot below:

from-to1

But to achieve this you need to create a query in a specific way. Usually in such case both parameters passed to the query are equal so if you want your report to work in an intuitive way you need to write this kind of SELECT:

SELECT
    MyDate
FROM
    MyTable alias
WHERE
    (alias.MyDate >= @DateFrom) AND
    (alias.MyDate < DATEADD(d, 1, @DateTo))

Here the MyDate should be greater or equal to DateFrom and less to DateTo (but the DateTo is added with one day).

Maybe it's obvious but I found many reports with the bug. developer usually do not care (or do not know) if the report shows what it should show. Baka (stupid in Japanese) developer.

If you want to provide open range of dates: 

  • start with infinite end   from-to2
  • end with no start from-to3
  • no date filtering at all from-to4

use the following snippet:

SELECT
    MyDate
FROM
    MyTable alias
WHERE
    (@DateFrom IS NULL OR alias.MyDate >= @DateFrom) AND
    (@DateTo IS NULL OR alias.MyDate < DATEADD(d, 1, @DateTo))

This blog entry will be useful at least for my colleagues.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted on Wednesday, October 31, 2007 9:00 AM |

Feedback

Gravatar

# re: SQL date filtering - between date from and to

This worked fantastic! Thanks so much.

Sean S,
10/14/2008 9:11 AM | Sean
Gravatar

# re: SQL date filtering - between date from and to

This helped me out. Thanks
6/9/2009 11:42 AM | Hari
Gravatar

# re: SQL date filtering - between date from and to

Great yaar, thanks for the amazing help .
6/18/2009 11:50 PM | Abhishek
Gravatar

# re: SQL date filtering - between date from and to

Awesome. This helped me a lot!
7/21/2009 4:52 AM | FiLL
Gravatar

# re: SQL date filtering - between date from and to

a lot fthanks to you, simply superb
8/16/2009 11:43 PM | karthik
Gravatar

# re: SQL date filtering - between date from and to

Thanks a lot!
2/23/2010 9:22 AM | Curro
Gravatar

# Data filtering

i want to filter my records that contain same name, same date but different amount by just selecting only the name and store it in a dbgrid as one line after calculating the total amount.
5/9/2010 10:37 PM | Babs
Gravatar

# re: SQL date filtering - between date from and to

THANKS SO MUCH....U saved me a whole weeks worth of effort.. God bless u.
7/2/2010 2:34 AM | Junia
Gravatar

# re: SQL date filtering - between date from and to

Hi I have a query. I have two dates in database check in date and check out date . on the basis of these dates I have to get the price from database. I set price for those dates. like I set Price for 2011-01-01 to 2011-12-31.

when I retrieve It does not show anything.
SELECT * FROM tariff_infoT WHERE datefrom between '2011-02-02' and '2011-02-05'

I want to get price in any date between dates I have set in the database.

7/12/2011 9:55 PM | subhash
Gravatar

# re: SQL date filtering - between date from and to

Witaj Marcin -

twoj kawalek codu bardzo mi pomogl - chcialem podziekowac tutaj za madre rowiazanie - wlasnie sie meczylem z tym problemem budujac reporting w naszej firmie. Wiedzialem ze jest na to sposob, poniewaz w poprzedniej firmie tez bylo to rozwiazane. Dobrze ze znalalzem twoj blog.

Pozdrawiam z Nowego Yorku

Marcin Zietarski
10/5/2011 2:29 AM | Martin Zietarski
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: