我有一个具有以下结构的源表:
create table customer_info
(customer_num number,
birth_date char(10))
不幸的是,birth_date
列数据类型char(10)
不是date
. 此表的一些示例数据如下所示:
customer_num | birth_date
--------------------------------
1 | 2001/01/01
1 | 2010/01/01
1 | 2021/01/01
1 | 12hhhhuu6 --> Incorrect date
1 | 2001/01/01
1 | 2001/23/01 --> Incorrect date
我所做的是编写一个函数来评估每条记录并返回它的正确格式,但是如您所知,对每条记录使用一个函数并不是一个好主意,它会以某种方式降低性能。我想知道您是否可以为此提出更好的方法。
create or replace function new_to_date_en(d varchar2) return DATE
is
v_date date;
begin
select to_date(d,'yyyy/mm/dd' ) into v_date from dual;
return to_date(d,'yyyy/mm/dd');
exception when others then return to_dateto_date('2021/03/07', 'yyyy/mm/dd');
end;
使用功能:
select customer_num,
new_to_date_en(birth_date)
from customer_info;
T-SQL 中有一种方法COALESCE(TRY_CONVERT(date, @date, 111), '2012-01-01')
。oracle plsql中有类似的方法吗?
提前致谢
TRY_CONVERT
是为每条记录调用的函数,有什么不同?以下工作自 12.2 起:
在您的版本上,您可以使用您的功能。