Solved

oracle date question

Posted on 2014-04-17
14
447 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
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 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.

752 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