Solved

oracle date comparision

Posted on 2010-11-29
3
401 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 74

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 74

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
Suggested Courses

623 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