Oracle date comparison

Posts   
 
    
jookyone avatar
jookyone
User
Posts: 104
Joined: 25-Jan-2005
# Posted on: 30-Mar-2005 02:04:38   

I need to compare a date value input by the user to a datetime value in the database (Oracle 9i). How can I do the comparison using only the date portion and not the time portion? i.e. 2005-11-23 00:00:00 = 2005-11-23 23:24:29 should be compared as 2005-11-23 = 2005-11-23...or just have both time portions be "00:00:00" so that in effect the time portion won't influence the comparison. Any way I can do this using Predicates?

jookyone avatar
jookyone
User
Posts: 104
Joined: 25-Jan-2005
# Posted on: 30-Mar-2005 03:46:13   

After looking through the source for ORMSupportClasses, it hit me that of course the parameter type is determined by the underlying type of the value object passed in to create each parameter in the Predicate classes. My problem was that I was mistakenly thinking the type that would be passed in would be a default type, such as VARCHAR2 so a direct comparison <date field> = <parameter value> would not work. But if I were to do a CompareValuePredicate and pass in a DateTime object as the value to compare the comparison would succeed. The problem with the time portion still lingers though, unless I'm mistaken when you subtract one date field from another date field in Oracle the result is the difference in number of days. Since this is a FLOAT, it could be a fraction so I would need to add another expression (perhaps MOD) to disregard the partial number in the comparison.

Am I making this way too complex or am I on the right track?

jookyone avatar
jookyone
User
Posts: 104
Joined: 25-Jan-2005
# Posted on: 30-Mar-2005 04:47:11   

Just discovered an interesting way of comparing 2 date values in Oracle exclusive of the time portion--use LIKE. Curiously enough, using the LIKE operator with 2 date values will evaluate to true if the dates are equal (time portions notwithstanding) and false otherwise. Unfortunately, using the FieldLikePredicate won't work because it will always use VARCHAR2 as the parameter datatype. :-\

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 30-Mar-2005 10:03:56   

You could use a Between predicate simple_smile That's how it's done on SqlServer anyway (where you always have a time part in a date field). You set the first value to compare with to 00:00 for the time and the second value's time to 23:59:59.

Though oracle has a native 'date' type, without time. Perhaps that type is better for your application?

Frans Bouma | Lead developer LLBLGen Pro
jookyone avatar
jookyone
User
Posts: 104
Joined: 25-Jan-2005
# Posted on: 30-Mar-2005 15:30:01   

that might be easier, yea simple_smile We don't have a need for the timestamp, just the date. I'll see if we can switch those columns to that type and make it easier on myself, hehe. Thanks