Select date ranges with NULL in "toDate"

Dag Sunde

I have the following table:

PriceId PriceType  FromDate   ToDate         Price  ElementId
1           F      2014-01-01  NULL          0.00900      1
3           F      2014-01-01  2014-02-01    0.31470      4
4           F      2014-01-01  NULL        432.00000      3
6           F      2014-02-01  NULL          0.30950      4
7           F      2014-02-01  NULL          0.28990      2

The input is ElementID = 4, a fromDate and a toDate

I want all prices for an element where the from and todate are between the input period. The toDate are to, but not including.

So, 2014-01-28 to 2014-03-01 should return record 3 & 6 2014-01-28 to 2014-02-01 should only return record 3. 2014-02-07 to 2014-03-01 should return record 6.

Can anyone help me complete the WHERE clause:

 WHERE ElementId = 4 AND...

TIA...

-- Dag Sunde.

O. Jones

It is not entirely clear what the significance of a NULL ToDate is intended to be. I'll assume it means 'ongoing' or, basically , NOW().

In that case, you need

  WHERE whatever
    AND FromDate >= ?starting
    AND IFNULL(ToDate, NOW()) < ?ending

This will work correctly as long as your FromDate values are always in the past and before your ToDate values, and your ?starting parameters are before your ?ending.

There may be a serious efficiency problem with this design. It's hard to use indexes to search for null values.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Select Common Dates in Date Ranges

From Dev

How to select multiple date ranges from Fullcalendar?

From Dev

How to select distinct date ranges with Oracle SQL

From Dev

select date ranges for multiple years in r

From Dev

Select data from two date ranges SQL

From Dev

select statement for averages based on different date ranges in one MySQL query

From Dev

Select used days from multiple date ranges (events)

From Dev

momentjs toDate() date return not same as format() date

From Dev

MySQL select max date or null date

From Dev

MySQL select max date or null date

From Dev

check date ranges with other date ranges

From Dev

Select date with null value in 2 table

From Dev

Working with date ranges in SPSS

From Dev

Date ranges in Elixir?

From Dev

Storing multiple date ranges

From Dev

Get contiguous date ranges

From Dev

Multiple Date Ranges for an Employee

From Dev

Trouble with date ranges

From Dev

Merge date ranges

From Dev

Aggregating groups of date ranges

From Dev

Index/match for date ranges

From Dev

MySQL: Grouping by date ranges

From Dev

select more ranges in mysql

From Dev

Select cell ranges and then print

From Dev

select datas between ranges

From Dev

How to select data between two date ranges using datepicker c#

From Dev

Set Minimum date for DatePickerDialog getting IllegalArgumentException Fromdate does not precede toDate

From Dev

Retrieve a record with fromdate and todate by passing single date in MySQL

From Dev

toDate returns the Date in the users timezone instead of the parsed timezone