Solved

oracle date question

Posted on 2014-04-17
14
439 Views
Last Modified: 2014-04-17
We have an audit table and the date value is stored in a column called old_value which is a string. Is there a way to compare this string to a date column in the database?

Old_value   01/05/2014 00:00
0
Comment
Question by:anumoses
  • 7
  • 6
14 Comments
 
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 250 total points
ID: 40006975
To convert strings to dates use TO_DATE.

select to_date('01/05/2014 00:00','MM/DD/YYYY HH24:MI') from dual;

You just need to change the format mast to match the data.  I guess your time of 00:00 was 24 hour hours and minutes.

You can then use the TO_DATE value and compare it to any other dates.
0
 
LVL 6

Author Comment

by:anumoses
ID: 40007001
That is true. But the column old_value is varchar

select to_date(old_value,'MM/DD/YYYY HH24:MI') from blood_drives_audit

I get an error.
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40007008
>>I get an error.

I believe we've covered this in some of your previous questions.

You likely have a string in the column that isn't in the correct format for the format mask in the TO_DATE call doesn't match the data you actually have.

That is the problem with storing dates as strings.

You need to find the 'bad' data and clean it up.
0
 
LVL 6

Author Comment

by:anumoses
ID: 40007025
It's not about  the 'bad' data and clean it up. Its an audit table which stores data from main table when there is a delete or update. So your answer is NO it cannot be done.
0
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 250 total points
ID: 40007036
>>So your answer is NO it cannot be done.

Before providing a "B" grade and closing it why not give us a chance.

I disagree with "So your answer is NO it cannot be done.".

It CAN be done but we need more information.

I you want to continue just let me know and I'll unaccept this.
0
 
LVL 6

Author Comment

by:anumoses
ID: 40007038
This is how it is written to the audit table

IF NVL(:OLD.drive_date,TO_DATE('01-JAN-1801', 'DD-MON-YYYY')) !=
           NVL(:NEW.drive_date,TO_DATE('01-JAN-1801', 'DD-MON-YYYY'))
        THEN
INSERT INTO BLOOD_DRIVES_AUDIT (
                        audit_key ,
                        audit_insert_date ,
                        program_id ,
                        user_id  ,
                        action_code ,
                        column_name ,
                        old_value ,
                        new_value)
                   VALUES (
                        :OLD.audit_key ,
                        SYSDATE ,
                        NULL ,
                        USER ,
                        v_operation_c,
                        'drive_id' ,
                        TO_CHAR(:OLD.drive_date, 'MM/DD/YYYY HH24:MI'),
                        TO_CHAR(:NEW.drive_date, 'MM/DD/YYYY HH24:MI') );

End if;
0
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.

 
LVL 6

Author Comment

by:anumoses
ID: 40007042
I am sorry. Did not mean to do it. Can you open the question. Thanks,
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40007050
You mentioned an error before.

What was the error you received?
0
 
LVL 6

Author Comment

by:anumoses
ID: 40007064
select to_date(old_value,'MM/DD/YYYY HH24:MI') from blood_drives_audit

ORA-01843: not a valid month
0
 
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 250 total points
ID: 40007066
Run this quick pl/sql block to find the 'bad' dates:

declare
	mydate date;
begin

	for i in (select old_value from BLODD_DRIVES_AUDIT) loop
		begin
			myDate := to_date(i.old_value,'MM/DD/YYYY HH24:MI');
			exception when others then
				dbms_output.put_line(i.old_value || ' is not a valid date.');
		end;
	end loop;
end;
/

Open in new window

0
 
LVL 6

Author Comment

by:anumoses
ID: 40007086
You are correct

select to_date(old_value,'MM/DD/YYYY HH24:MI') from blood_drives_audit
where column_name = 'drive_id'

I had to add column_name since all different values are there in the audit table.

Not I dont get any error.
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40007090
Glad you were able to figure it out.
0
 
LVL 6

Author Closing Comment

by:anumoses
ID: 40007094
thanks
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

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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

758 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

24 Experts available now in Live!

Get 1:1 Help Now