Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Dates in 2 Fields, which is greater then where

Author  Topic 

BobFoutFX4
Starting Member

9 Posts

Posted - 2008-03-03 : 14:16:47
I have two date fields in my table. Date1 and Date2. I need to pick the date that is greater between the two fields, and then use that date as criteria in the select statement. So the resulting larger date would need to fall within a certain range of dates... say between 12/1/2007 and 12/31/2007.

Example1: Date1=11/30/2007 and Date2=12/18/2007. The greater date would be 12/18 and this record would be included.

Example2: Date1=12/15/2007 and Date2=01/05/2008. The greater date would be 01/05 and this record would not be included.

Any help is much appreciated!

jhocutt
Constraint Violating Yak Guru

385 Posts

Posted - 2008-03-03 : 14:35:45
declare @a table (
date1 datetime,
date2 datetime
)

insert into @a select '11/30/2007' , '12/18/2007' union all select '12/15/2007', '01/05/2008'

select *
from @a
where case
when date1 > date2 then date1
else date2
END
between '12/1/2007' and '12/31/2007'



"God does not play dice" -- Albert Einstein
"Not only does God play dice, but he sometimes throws them where they cannot be seen."
-- Stephen Hawking
Go to Top of Page

BobFoutFX4
Starting Member

9 Posts

Posted - 2008-03-03 : 14:52:39
I think that worked! Thanks so much!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-03-04 : 07:46:34
Alsways express dates in YYYYMMDD format to avoid conflict with local date settings

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -