|
|
Re: plsql function which gives error if i enter the invalid date [message #263607 is a reply to message #263597] |
Thu, 30 August 2007 08:48 |
|
Littlefoot
Messages: 21823 Registered: June 2005 Location: Croatia, Europe
|
Senior Member Account Moderator |
|
|
I believe you should know input value format; if you don't restrict that, it will become an impossible mission. For example, will this function work for (today)
- 30.08.2007
- 30.08.2007.
- 08/30/07
- 30-aug-07
- 2454343 (today as Julian date)
- etc.
Once this decision is made (for example, let's use DD.MM.YYYY as acceptable format), we could think about the next step - deciding whether this value is valid or not. One way to do it could be as follows: try to convert input string into a date; if it is possible, return 'VALID'; if not, TO_DATE function will fail, raise an exception and return 'INVALID'.CREATE OR REPLACE FUNCTION Is_Date (par_date IN CHAR)
RETURN CHAR
IS
l_date DATE;
BEGIN
l_date := TO_DATE(par_date, 'dd.mm.yyyy');
RETURN ('VALID');
EXCEPTION
WHEN OTHERS THEN
RETURN ('INVALID');
END;
/
select is_date('30.08.2007') d1, is_date('32.08.2007') d2 from dual;
D1 D2
---------- ----------
VALID INVALID
|
|
|
|
|
|
|
|
function to raise error [message #263921 is a reply to message #263597] |
Fri, 31 August 2007 05:11 |
nagaraj42
Messages: 8 Registered: August 2007 Location: chennai
|
Junior Member |
|
|
Can anyone please tell me how to use the SQLERRM function in the exception to raise an error
The query is
CREATE OR REPLACE FUNCTION BDSDATA.VALID_DATE ( PAR_DATE VARCHAR2)
RETURN VARCHAR2 IS OUT_DATE DATE;
BEGIN
OUT_DATE:= TO_DATE ( PAR_DATE, 'DD/MM/YYYY');
RETURN OUT_DATE ;
EXCEPTION
WHEN OTHERS THEN
raise_application_error(SQLERRM);
END ;
/
|
|
|
|
|
|
|
|
|
|
|
|