oracle timestamp

Hi experts,
I need to update a column in my table based on the system timestamp - 30 days.

the column is defined as varchar(50)
and when I select the values from the column I get the output as

2015-08-28 03:33:57
2015-09-21 02:25:17
2015-09-21 02:24:54

there is another column called 'compliant' which is defined as number and takes a binary value 1 or 0

I need to set this column to 1 if the timestamp in the varchar column is  current_date- 30

I tried this ---

update RTC_STG
set iscompliant = 1
where to_timestamp(lastupdatetime,'YYYY-MM-DD 24HH:MM:SS') = current_date - 30


and I get this error --

Error report:
SQL Error: ORA-01841: (full) year must be between -4713 and +9999, and not be 0
01841. 00000 -  "(full) year must be between -4713 and +9999, and not be 0"
*Cause:    Illegal year entered
*Action:   Input year in the specified range

how do I fix this issue??
smaligAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
First change:
to_timestamp(lastupdatetime,'YYYY-MM-DD 24HH:MM:SS')

to:
to_timestamp(lastupdatetime,'YYYY-MM-DD HH24:MM:SS')


If you still get an illegal year, you have bad data.

Second:
current_date - 30 means 30 days down to the SECOND you execute that statement.  If that is what you want, then great but I really doubt it.

Since your lastupdatetime has the time populated, what do you want to update on?
0
smaligAuthor Commented:
there are 2 columns in the table. lastupdatetime, iscompliant.

lastupdate time has the time populated. iscompliant column needs to be updated to 1 if the lastupdatetime values is <= current_date - 30
or updated to 0 if it is greater
0
slightwv (䄆 Netminder) Commented:
>>iscompliant column needs to be updated to 1 if the lastupdatetime values is <= current_date - 30

update RTC_STG  set iscompliant = case when to_timestamp(lastupdatetime,'YYYY-MM-DD HH24:MM:SS') <= current_date-30 then 1 else 0 end;

This still won't fix the data problem.  The error above is 99% a data issue.  This is why you should never store dates as a string.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

smaligAuthor Commented:
I am getting this error now --

Error report:
SQL Error: ORA-01841: (full) year must be between -4713 and +9999, and not be 0
01841. 00000 -  "(full) year must be between -4713 and +9999, and not be 0"
*Cause:    Illegal year entered
*Action:   Input year in the specified range
0
slightwv (䄆 Netminder) Commented:
>>I am getting this error now --

Bad data.

Here is a simple pl/sql loop to find it.  I set up my own table and data as an example.

drop table tab1 purge;
create table tab1(lastupdatetime varchar2(20));

insert into tab1 values('Hello');
insert into tab1 values('2015-08-28 03:33:57');
insert into tab1 values('2015-09-21 02:25:17');
insert into tab1 values('2015-09-21 02:24:54');
commit;

declare
	junk date;
begin
	for i in (select rowid, lastupdatetime from tab1) loop
		begin
			junk := to_date(i.lastupdatetime,'YYYY-MM-DD HH24:MI:SS');
			exception when others then
				dbms_output.put_line(i.lastupdatetime || ' is bad and the rowid is ' || i.rowid);
		end;
	end loop;
end;
/

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
smaligAuthor Commented:
Awesome. was able to get the bad data out and update worked. Thanks a lot.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.