Solved

oracle date comparision

Posted on 2010-11-29
3
398 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle 12c patching 1 80
Oracle - SQL Parse String 5 33
Oracle SQL - Query help 7 54
oracle- set role and grant privileges 6 28
Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

816 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

10 Experts available now in Live!

Get 1:1 Help Now