Solved

oracle date comparision

Posted on 2010-11-29
3
397 Views
Last Modified: 2012-05-10
experts... how do i compare two dates in oracle. I have 2 variables
first(v_date1) of varchar2(30) where I get a date in the format 'yyyy-mm-dd hh24:mi' and have another variable(v_date2) of varchar2(30) where i get a value from a table of 'date' column type by doing
select to_char(i_date )
into v_date2
from table1
where col1 = some_value

I want to see if v_date2 is less than v_date1.  How do i do this????
0
Comment
Question by:chickanna
  • 2
3 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 34233294
easiest  (and most accurate) way is to NOT convert your dates to strings.

declare v_date2 as a DATE,  not a varchar2.
and just select i_date not to_char(i_date)

since your v_date1 is already a string though,  conver it to a date and compare it

if to_date(v_date1,'yyyy-mm-dd hh24:mi')  < v_date2 then
.....
end if;


0
 
LVL 20

Expert Comment

by:gatorvip
ID: 34233300
convert to date, then compare them directly:


 to_date(v_date1, 'yyyy-mm-dd hh24:mi') > to_date( v_date2, 'yyyy-mm-dd hh24:mi' )
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 34233318
if you must have both variables as string type  then force the v_date2 to have a consistent format

to_char(i_date,'yyyy-mm-dd hh24:mi')

that format should allow for direct string comparison.

or you could convert both back to dates with

to_date(v_date1,'yyyy-mm-dd hh24:mi')

to_date(v_date2,'yyyy-mm-dd hh24:mi')
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now