-
Notifications
You must be signed in to change notification settings - Fork 18
/
is_number.sql
71 lines (66 loc) · 1.83 KB
/
is_number.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
-- ----------------------------------------------------------------------------------------------
--
-- Script: is_number.sql
--
-- Author: Adrian Billington
-- www.oracle-developer.net
--
-- Description: A couple of variations of an IS_NUMBER function. This is a common approach that
-- can certainly be found in at least one well-known PL/SQL book.
--
-- a) SQL and PL/SQL Version
-- ----------------------
-- Returns 1 if TRUE or 0 if FALSE.
--
-- Example usage:
--
-- SELECT is_number( {char_column} )
-- FROM ...;
--
-- SELECT ...
-- FROM ...
-- WHERE is_number(char_column) = 1;
--
-- b) PL/SQL-only Version
-- -------------------
--
-- boolean_var := is_number(char);
--
-- IF is_number(char) THEN...
--
-- Version: 1.0: original
-- 1.1: removed deterministic keyword (thanks to Matthias Rogel)
--
-- ----------------------------------------------------------------------------------------------
--
-- SQL and PL/SQL version. Returns 1 if TRUE or 0 if FALSE...
--
CREATE FUNCTION is_number (
str_in IN VARCHAR2
) RETURN NUMBER PARALLEL_ENABLE IS
n NUMBER;
BEGIN
n := TO_NUMBER(str_in);
RETURN 1;
EXCEPTION
WHEN VALUE_ERROR THEN
RETURN 0;
END;
/
--
-- PL/SQL-only version (returns BOOLEAN)...
--
CREATE FUNCTION is_number (
str_in IN VARCHAR2
) RETURN BOOLEAN PARALLEL_ENABLE IS
n NUMBER;
BEGIN
n := TO_NUMBER(str_in);
RETURN TRUE;
EXCEPTION
WHEN VALUE_ERROR THEN
RETURN FALSE;
END;
/
CREATE OR REPLACE PUBLIC SYNONYM is_number FOR is_number;
GRANT EXECUTE ON is_number TO PUBLIC;