Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

oracle date question

Posted on 2014-04-17
14
Medium Priority
?
452 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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
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 how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
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.

721 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