如何测试日期格式字符串是否是 Oracle 中的有效日期格式字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7979654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 03:40:24  来源:igfitidea点击:

How to test if date format string is a valid date format string in Oracle

oracleplsql

提问by Rene

I want users to be able to enter a date format string so they can specify how they want a date value to be displayed/entered.

我希望用户能够输入日期格式字符串,以便他们可以指定他们希望如何显示/输入日期值。

How can I validate this date format string so that they can enter only a valid Oracle date format strings?

如何验证此日期格式字符串,以便他们只能输入有效的 Oracle 日期格式字符串?

回答by Kevin Burton

you could create a function:

你可以创建一个函数:

e.g:

例如:

FUNCTION is_valid_date_format ( 
    p_format IN VARCHAR2 ) 
RETURN BOOLEAN IS
    l_date VARCHAR2(100) := NULL;
BEGIN
    l_date := TO_char( sysdate, p_format );
    RETURN TRUE;
EXCEPTION
    WHEN OTHERS THEN
        RETURN FALSE;
END is_valid_date_format;

and use it like this

并像这样使用它

IF is_valid_date_format('dd/mm/yyyy') THEN

at the moment it will allow time formats too, however it would be simple to extend it to disallow a format that contains undesired formats e.g: hh hh24 mi ss

目前它也将允许时间格式,但是扩展它以禁止包含不需要的格式的格式很简单,例如:hh hh24 mi ss

by adding: (you will probably want to uppercase your format string first)

通过添加:(您可能希望先将格式字符串大写)

IF INSTR(p_format,'HH')>0 OR INSTR(p_format,'HH24')>0 
OR INSTR(p_format,'MI')>0  OR INSTR(p_format,'SS')>0 THEN
    RETURN FALSE
END IF;