Wednesday, March 28, 2012

Help on SQL Query

Hi all,

I need some help on sql query.

I had a table named "tableA" where by I had a "timestart" and "timeend" field.

I would like to query all the records between timestart>= '2/2/2007 2:00:00 AM' and timeend<= '2/2/2007 3:00:00 AM' and also

timestart>= '2/3/2007 4:00:00 AM' and timeend<= '2/3/2007 5:00:00 AM'

Can I do this in a single sql statement instead of querying the table twice?

Please help.

Thanks

Yes you can

select *
from tableA
where (timestart>= '2/2/2007 2:00:00 AM' and timeend<= '2/2/2007 3:00:00 AM' )
and (timestart>= '2/3/2007 4:00:00 AM' and timeend<= '2/3/2007 5:00:00 AM' )

but in your example you will receive empty result because it is not possible for date to be valid for this query so maybe you mind this

select *
from tableA
where (timestart>= '2/2/2007 2:00:00 AM' and timeend<= '2/2/2007 3:00:00 AM' )
or (timestart>= '2/3/2007 4:00:00 AM' and timeend<= '2/3/2007 5:00:00 AM' )

Thanks

No comments:

Post a Comment