Home » SQL & PL/SQL » SQL & PL/SQL » Date Updation (Oracle 12C Windows NT)
Date Updation [message #650854] |
Thu, 05 May 2016 09:03 |
pstanand
Messages: 133 Registered: February 2005 Location: Chennai,India
|
Senior Member |
|
|
Hi,
I have a table products which is having lastupdate column with varchar2(29).
It contains values with the mixture of 'MM/DD/YYYY' and 'YYYY-MM-DD'.
When I ran min(lastupdate),max(lastupdate) from products I got invalid results ,as I have maximum date value as '2016-04-28'.
But it shows maximum lastupdate as '09/09/2015' instead '2016-04-28'.
Incase if I want to update the lastupdate column to get all the datevalues in the format 'YYYY-MM-DD' how can I do that?
I have tried TO_DATE and TO_CHAR combinations. Below is the error result I got.
Kindly let me know how to resolve this.
Error report -
SQL Error: ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
*Action: Specify a valid number.
update products
set lastupdate=to_char(lastupdate,'YYYY-MM-DD')
Thanks.
Regards,
Pstanand.
|
|
|
Re: Date Updation [message #650855 is a reply to message #650854] |
Thu, 05 May 2016 09:09 |
John Watson
Messages: 8930 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
You'll have to write some pattern recognition code that parses the data to work out what it actually means. There is no built-in function to do that.
|
|
|
|
|
|
Re: Date Updation [message #650871 is a reply to message #650861] |
Thu, 05 May 2016 13:44 |
Bill B
Messages: 1971 Registered: December 2004
|
Senior Member |
|
|
If you are not comfortable with regular expressions (use them, their great). The following code will work also
1 with
2 data as (
3 select '09/09/2015' v from dual
4 union all
5 select '2016-04-28' from dual)
6 select to_date(v,'MM/DD/YYYY')
7 FROM DATA
8 WHERE INSTR(V,'/') > 0
9 UNION ALL
10 SELECT TO_DATE(V,'YYYY-MM-DD')
11 FROM DATA
12 WHERE INSTR(V,'-') > 0
13* ORDER BY 1
14 /
TO_DATE(V
---------
09-SEP-2015
28-APR-2016
Like Michel said, get in the habit of ALWAYS storing dates in a date column. Never use a string.
[Updated on: Thu, 05 May 2016 13:49] Report message to a moderator
|
|
|
|
|
Re: Date Updation [message #650896 is a reply to message #650854] |
Fri, 06 May 2016 05:50 |
pstanand
Messages: 133 Registered: February 2005 Location: Chennai,India
|
Senior Member |
|
|
Hi All,
Thanks for your suggestions. However the lastupdate column is defined in SQL Server is Varchar and so we have to keep it in oracle.
And when we moved the data some of them are looks as I mentioned earlier.
Based on the inputs from this thread I found the date with values '/' and framed this code piece.
This code is running for about 40 minutes and not completing. The table contains 4 million records.
Kindly request you to validate my code and correct me where I'm wrong and provide a solution,so that to update the column as expected 'YYYY-MM-DD'.
Many Thanks for your support.
DECLARE
CURSOR CUR_DATE_VAL
IS
SELECT (year_val||'-'||month_val||'-'||date_val)dateval,
id
FROM ( SELECT lastupdate,id,
substr(substr(lastupdate,instr(lastupdate,'/',-2)+1),1,4)year_val,
substr(lastupdate,1,instr(lastupdate,'/',1)-1) month_val,
substr(lastupdate,instr(lastupdate,'/',1)+1,(instr(lastupdate,'/',1,2)-instr(lastupdate,'/',1))-1)date_val
FROM sap_products_country_test)
WHERE month_val IS NOT NULL
AND date_val IS NOT NULL;
TYPE date_val_tab IS TABLE OF cur_date_val%ROWTYPE;
l_date_val_tab date_val_tab;
BEGIN
OPEN cur_date_val;
FETCH cur_date_val BULK COLLECT INTO l_date_val_tab LIMIT 1000;
CLOSE cur_date_val;
IF l_date_val_tab.COUNT > 0
THEN
FOR i in l_date_val_tab.FIRST..l_date_val_tab.LAST
LOOP
UPDATE sap_products_country_test
SET lastupdate = l_date_val_tab(i).dateval
WHERE id = l_date_val_tab(i).id;
END LOOP;
END IF;
EXCEPTION
WHEN OTHERS
THEN
RAISE_APPLICATION_ERROR(-20001,SQLERRM);
END;
|
|
|
Re: Date Updation [message #650897 is a reply to message #650896] |
Fri, 06 May 2016 05:56 |
pstanand
Messages: 133 Registered: February 2005 Location: Chennai,India
|
Senior Member |
|
|
Hi All,
I have created a test code like the below and tested.
It was got updated as expected. After that only I tried this with my main table contains 4 million records in my previous post.
And it is not completed its execution.
So requesting your advice in this regard.
Thanks
pstanand.
CREATE TABLE date_test(dateval varchar2(20),id number);
insert into date_test(dateval) values('05/05/2016',1);
insert into date_test(dateval) values('2016-05-04',2);
insert into date_test(dateval) values('4/30/2016',3);
insert into date_test(dateval) values('2/21/2012',4);
insert into date_test(dateval) values('1/13/2015',5);
insert into date_test(dateval) values('1/12/2016',6);
insert into date_test(dateval) values('7/9/2013',7);
insert into date_test(dateval) values('2011-11-29',8);
insert into date_test (dateval)values('2014-11-25',9);
insert into date_test (dateval) values('2014-11-18',10);
insert into date_test(dateval) values('3/10/2015',11);
declare
cursor cur_date_val is
select (year_val||'-'||month_val||'-'||date_val)dateval,
month_val,
date_val,
id
from ( select dateval,id,
substr(substr(dateval,instr(dateval,'/',-2)+1),1,4)year_val,
substr(dateval,1,instr(dateval,'/',1)-1) month_val,
substr(dateval,instr(dateval,'/',1)+1,(instr(dateval,'/',1,2)-instr(dateval,'/',1))-1)date_val
from date_test)
where month_val is not null
and date_val is not null;
TYPE date_val_tab IS TABLE OF cur_date_val%ROWTYPE;
l_date_val_tab date_val_tab;
begin
OPEN cur_date_val;
FETCH cur_date_val BULK COLLECT INTO l_date_val_tab;
CLOSE cur_date_val;
IF l_date_val_tab.COUNT > 0
THEN
FOR i in l_date_val_tab.FIRST..l_date_val_tab.LAST
LOOP
dbms_output.put_line('Date Value: '||l_date_val_tab(i).dateval);
update date_test
set dateval = l_date_val_tab(i).dateval
where id = l_date_val_tab(i).id;
END LOOP;
END IF;
exception
when others then
raise_application_error(-20001,sqlerrm);
end;
|
|
|
|
Re: Date Updation [message #650900 is a reply to message #650899] |
Fri, 06 May 2016 06:58 |
pstanand
Messages: 133 Registered: February 2005 Location: Chennai,India
|
Senior Member |
|
|
Hi,
The dates were stored as MM/DD/YYYY format only and so I created the above code piece to update it.
could you please validate and provide me a solution.
Thanks.
|
|
|
Re: Date Updation [message #650901 is a reply to message #650900] |
Fri, 06 May 2016 07:01 |
Roachcoach
Messages: 1576 Registered: May 2010 Location: UK
|
Senior Member |
|
|
The solution is to store it as a date and convert it properly. You're asking for a major foul up in the future doing this.
The developers will push back, of course, but you really need to put your foot down here.
|
|
|
|
Re: Date Updation [message #650906 is a reply to message #650905] |
Fri, 06 May 2016 07:48 |
Bill B
Messages: 1971 Registered: December 2004
|
Senior Member |
|
|
I have been programming computers for 40 year from mainframes in assembler to relational databases and I'm telling you that when the data is pulled over from sql server, use a DATE column to store the date. Do NOT use a string!!!
to convert from the string into a date simply use
to_date(date_val,'MM/DD/YYYY')
To convert to a string for going back to sql server or displaying it.
to_char(MY_DATE_COLUMN,'MM/DD/YYYY')
It's not that we won't help you, we are trying to stop you from making a horrible mistake.
[Updated on: Fri, 06 May 2016 07:49] Report message to a moderator
|
|
|
|
Goto Forum:
Current Time: Wed Apr 24 00:48:29 CDT 2024
|