tags:

views:

45

answers:

2

I have a table with 3 fields:

  • id
  • datestart
  • dateend

I need to query this to find out if a pair of dates from a form are conflicting i.e

table entry 1, 2010-12-01, 2010-12-09

from the form 2010-12-08, 2010-12-15 (edited, typo)

select id 
  from date_table  
 where '2010-12-02' between datestart and dateend

That returns me the id that I want, but what I would like to do is to take the date range from the form and do a query similar to what I have got that will take both form dates 2010-12-08, 20-12-15 and query the db to ensure that there is no conflicting date ranges in the table.

Am sat scratching my head with the problem...

TIA

A: 
select id from date_table where '2010-12-02' between datestart and dateend;

I am not aware of the scope of your program. But, it seems like you're saying: select id from table where this date "2010-12-02" is in between 2 variables. I think you want to select where datestart is between the 2 variables.

Forgive me & correct me if I am wrong? You probably want something like this:

SELECT id FROM date_table WHERE datestart = datestart AND dateend = dateend;

if that's the case you probably just want a count:

SELECT COUNT(id) FROM date_table WHERE datestart = datestart AND dateend = dateend;

Good luck!

Auston
In plain english what am needing is to ensure that the date range from the form `form_start` to `form_end` does not overlap with the date ranges in the table `datestart` to `dateend`Maybe some extra info would help, am doing this for my boss (I'm a welder !) and we use a number of fitting crews to install steelwork job, we want to be able to get a list `id` of crews that are available...Maybe that will help a bit ?
Simon
`select id from date_table where '2010-12-02' between datestart and dateend;`should read something like (in english)`SELECT id FROM date_table WHERE RANGE form_date_start TO form_date_end IN range datestart TO dateend`So that "should" give me a list of `id` that is busy on a job at sometime in the date range...
Simon
A: 

If I've understood your request correctly you would like a list of all entries in date_table that either overlaps your form_start, your form_end or is entirely contained within the form_start - form_end date range.

So your query should look something like this:

SELECT id FROM
date_table
WHERE
(datestart < form_start AND dateend => form_start)
OR
(datestart <= form_end AND dateend > form_end)
OR
(datestart >= form_start  AND dateend <= form_end)
Ivar Bonsaksen
@Ivar, thanks for that, it got me thinking about the problem and sort of partially resolved it myself... (was full resolved until my boss threw me a curve ball).. Overlap is allowed, as the fitting crew finishes jobs in the morning and can be scheduled to start another in the afternoon !!!`SELECT av_bookedid, av_datestart, av_dateendFROM date_tableWHERE ('form_start' BETWEEN datestart AND dateend) OR ('form_end'BETWEEN datestart AND dateend);`
Simon
I don't know if this applies to your scenario, but you'd still miss those form dates overlapping entire date spans in the date_table.You could catch those by adding an OR (form_start < datestart AND dateend < form_end)
Ivar Bonsaksen
Thats the bit I need !!! You are a GOD !!!!!crazy that something so simple can feel so far away ...Thank you so much my friend
Simon