PL / SQL检查日期有效 [英] PL/SQL check date is valid
本文介绍了PL / SQL检查日期有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我在PL / SQL中有日期格式 DDMM
,并且我想验证它。什么是正确的方法?
If I have a date format DDMM
in PL/SQL and I want to validate it. What is the correct way to do it?
DD
是一天, MM
是蛾子。
For an example:
0208 - is a valid date
3209 - is not a valid date
0113 - is not a valid date.
推荐答案
你可以编写一个这样的函数, :
You could write a function like this one, for instance:
create or replace function is_valid(p_val in varchar2)
return number
is
not_a_valid_day exception;
not_a_valid_month exception;
pragma exception_init(not_a_valid_day, -1847);
pragma exception_init(not_a_valid_month, -1843);
l_date date;
begin
l_date := to_date(p_val, 'ddmm');
return 1;
exception
when not_a_valid_day or not_a_valid_month
then return 0;
end;
SQL> with test_dates(dt) as(
2 select '0208' from dual union all
3 select '3209' from dual union all
4 select '0113' from dual
5 )
6 select dt, is_valid(dt) as valid
7 from test_dates
8 /
DT VALID
---- ----------
0208 1
3209 0
0113 0
这篇关于PL / SQL检查日期有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文