Groups | Blog | Home
all groups > sql server programming > march 2007 >

sql server programming : DATE RANGE PROBLEM


Ram
3/13/2007 9:52:00 PM
Hi All,
I have below table structure:

ItemID Price FromDate
ToDate

---------------------------------------------------------------------------------------------
1 10.00 01/01/2005
12/31/2005
1 9.00 01/01/2004
12/31/2004
2 2.00 01/01/1900
01/01/9999
---------------------------------------------------------------------------------------------

Now I need to write a query to get price for all the items for the date
range 01/01/2005 to 12/31/2005



If I use FromDate >= 01/01/2005 and ToDate <= 01/01/2005 then I will get
only one record (ItemID=1). But ItemID =2 also lies in the same daterange,
ie, daterange specified lies between 1900 and 9999. But the query will not
give that result.

Please let me know how to get this record also.

Thanks in advance
Jack Vamvas
3/14/2007 12:00:00 AM
Add another condition on your SELECT statement , that will satisfy the extra
date requirement

--

Jack Vamvas
___________________________________
The latest IT jobs - www.ITjobfeed.com
<a href="http://www.itjobfeed.com">UK IT Jobs</a>


[quoted text, click to view]

Tom Cooper
3/14/2007 1:30:21 AM
It depends on what you mean by price in the date range 01/01/2005 to
12/31/2005. Do you mean the range must include ALL of the dates in that
range or do you mean it must include at least one date in the range. That
is, if the ToDate is March 1, 2005 and the FromDate is March31, 2005, do you
want to include it.

If you mean the range must include all dates in that range, then use

Where FromDate <= '20050101' And ToDate >= '20051231'

If you mean the range must include at least one date in that range, then

Where FromDate < '20060101' And ToDate >= '20050101'

Tom

[quoted text, click to view]

Uri Dimant
3/14/2007 7:21:40 AM
Ram

create table #t (
itemid int, price decimal(5,2),
fromdate datetime, todate datetime)

insert into #t values (1,10,'20050101','20051231')
insert into #t values (1,9,'20040101','20041231')
insert into #t values (2,2,'19000101','99990101')


How does year 19000101 relate to your condition from january 2005 to
december 2005?

Try this one

select * from #t
where fromdate >='20050101' or( todate >='20051231' and todate <='99990101')




[quoted text, click to view]

AddThis Social Bookmark Button