Need help with insert in Oracle [message #668311] |
Sat, 17 February 2018 13:53  |
 |
victor_pulga
Messages: 3 Registered: February 2018
|
Junior Member |
|
|
I have a table which i need to be populated.
The data is coming from another table in which the date columns are designed as Varchar2(10) : (eg: 2017-03-29)
My target table has date column defined as DATE.
Now what I want to do is to set a default value to be entered into the target DATE column, when I'm receiving any other data than the date(like 'UUUUUUUUUU').
Is there any way?
|
|
|
|
|
Re: Need help with insert in Oracle [message #668315 is a reply to message #668313] |
Sat, 17 February 2018 20:20   |
Solomon Yakobson
Messages: 3244 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
This will error out if year is 0 and/or month is 00 or > 12 and/or day is 00 or > max day for the month/year. It is simpler to create a function:
create or replace
function yyyy_mm_dd(
p_date_string varchar2
)
return date
is
begin
return to_date(p_date_string,'yyyy-mm-dd');
exception
when others
then
return null;
end;
/
Now we can:
SQL> alter session set nls_date_format='yyyy-mm-dd'
2 /
Session altered.
SQL> with t as (
2 select '2018-02-17' ds from dual union all
3 select '20-17-2018' from dual union all
4 select 'abc' from dual
5 )
6 select ds,
7 yyyy_mm_dd(ds)
8 from t
9 /
DS YYYY_MM_DD
---------- ----------
2018-02-17 2018-02-17
20-17-2018
abc
SQL>
And for 12C:
SQL> alter session set nls_date_format='yyyy-mm-dd'
2 /
Session altered.
SQL> with
2 function yyyy_mm_dd(
3 p_date_string varchar2
4 )
5 return date
6 is
7 begin
8 return to_date(p_date_string,'yyyy-mm-dd');
9 exception
10 when others
11 then
12 return null;
13 end;
14 t as (
15 select '2018-02-17' ds from dual union all
16 select '20-17-2018' from dual union all
17 select 'abc' from dual
18 )
19 select ds,
20 yyyy_mm_dd(ds)
21 from t
22 /
DS YYYY_MM_DD
---------- ----------
2018-02-17 2018-02-17
20-17-2018
abc
SQL>
SY.
|
|
|
|
|
Re: Need help with insert in Oracle [message #668318 is a reply to message #668317] |
Sun, 18 February 2018 05:57   |
Solomon Yakobson
Messages: 3244 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
It just hit me I forgot Oracle does some "guessing" when converting to date:
SQL> select to_date('2012.10.11','yyyy-mm-dd') from dual
2 /
TO_DATE('2
----------
2012-10-11
SQL> select to_date('20121011','yyyy-mm-dd') from dual
2 /
TO_DATE('2
----------
2012-10-11
SQL>
Or worth:
SQL> select to_date('10201211','yyyy-mm-dd') from dual
2 /
TO_DATE('1
----------
1020-12-11
SQL>
So to_date format mask (both in my suggested function and in your suggested 12C solution) should use exact format checking modifier FX:
SQL> select to_date('2012.10.11','fxyyyy-mm-dd') from dual
2 /
select to_date('2012.10.11','fxyyyy-mm-dd') from dual
*
ERROR at line 1:
ORA-01861: literal does not match format string
SQL> select to_date('20121011','fxyyyy-mm-dd') from dual
2 /
select to_date('20121011','fxyyyy-mm-dd') from dual
*
ERROR at line 1:
ORA-01861: literal does not match format string
SQL> select to_date('10201211','fxyyyy-mm-dd') from dual
2 /
select to_date('10201211','fxyyyy-mm-dd') from dual
*
ERROR at line 1:
ORA-01861: literal does not match format string
SQL>
SY.
|
|
|
|
|
|
Re: Need help with insert in Oracle [message #668324 is a reply to message #668323] |
Sun, 18 February 2018 11:29   |
 |
Michel Cadot
Messages: 68501 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
"default <> on conversion error" clause is new in 12.2 so did not exist in 12.1
This explains this error.
Bow for your first point: "now all the results are returning as Null" you have to
1/ Show us what happens if you do it with the test case Solonmon provided ("with...")
2/ Post some data for your specific case which should work and does not and show us it does not as Solomon showed posting at the same time the original value and the "converted" one for each row.
And don't forget to FORMAT, there is now no excuse you don't do it, read the link and see our posts.
[Updated on: Sun, 18 February 2018 11:30] Report message to a moderator
|
|
|
|