Solved

oracle date question

Posted on 2014-04-17
14
446 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
[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
  • 7
  • 6
14 Comments
 
LVL 77

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 77

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 77

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
 
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 77

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 77

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 77

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Can i Import Access Table Into Oracle Using Toad 36 276
SQL Developer 6 75
join actual table rows based on the column 25 31
return value based on substr 10 48
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

756 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