Solved

oracle date question

Posted on 2014-04-17
14
440 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
levels for reporting 5 63
custome paging in C# & oracle using inline queries. 12 42
case statement in where clause 5 40
Oracle - SQL Parse String 5 16
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…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

929 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

13 Experts available now in Live!

Get 1:1 Help Now