IS_NUMBER function?
I wrote a very primitive function to check if the contents of a VARCHAR field is Numeric. The function simply checks if every character in the field is between 0 - 9.
I feel like this is a very inefficient way to check for numeric/non-numeric data.
Is there a better way to do it?
The problem is not in that members regular expression but in the way the forum treats square brackets if you don't include {noformat}{noformat} tags around the code.
The square brackets are in the code, you just can't see them because of the forum if the code hasn't been formatted. (just as it's done with your code) ;)
Similar Messages
-
Error in IS_NUMBER Function PL/SQL!!
Hi Experts,
I have a pl/sql function for checking the input is numeber or not and it needs to retrun BOOLEAN value. Here is the code for that,
CREATE OR REPLACE function is_number(in_var in varchar2)
return BOOLEAN
is
v_number number;
begin
if in_var IS NOT NULL THEN
v_number := to_number(in_var);
return TRUE; -- No exception, so is a number
else
return FALSE;
end if;
exception
when others then
RAISE;
end;
When i run the above function,
select is_number('1') from dual;
Its errored out saying,
ORA-06552: PL/SQL: Statement ignored
ORA-06553: PLS-382: expression is of wrong type
Please let me know where i am wrong.
Thanks,
GWrite it in this way:
CREATE OR REPLACE FUNCTION is_number (in_var IN VARCHAR2)
RETURN VARCHAR2
IS
v_number NUMBER;
BEGIN
IF in_var IS NOT NULL
THEN
v_number := TO_NUMBER (in_var);
RETURN 'true'; -- No exception, so is a number
ELSE
RETURN 'FALSE';
END IF;
EXCEPTION
WHEN OTHERS
THEN
RETURN 'false';
END;
SELECT is_number ('d')
FROM DUAL;
false
SELECT is_number ('1')
FROM DUAL;
true- - - - - - - - - - - - - - - - - - - - -
Kamran Agayev A. (10g OCP)
http://kamranagayev.wordpress.com -
Is there any way to provide a value to a function and check if the value is numeric, character, date or any other data type. I know about Dump function in sql, please guide me if there is some other method like specific function(s) as is_date, is_number etc
you can easily create your own is_number function:
create or replace function is_number(pinput in varchar2) returns boolean is
vholdnum number;
begin
vholdnum := pinput-1;
return TRUE;
exception
when others then
return FALSE;
end;
is_date would be harder as you would have to check every conceivable format mask for dates.
For is_char (i assume you mean not just a number) just reverse is_number:
create or replace function is_char(pinput in varchar2) returns boolean is
vholdnum number;
begin
vholdnum := pinput-1;
return FALSE;
exception
when others then
return TRUE;
end; -
Hi , Please help to get the rows which have a character in the PFNUM column for this table
EMPNU PFNUM NAME
100 222 rat
101 a33 sanu
102 4a4 rahul
our PL/SQL is
1 DECLARE
2 i number;
3 j number;
4 t varchar2(10);
5 CURSOR ratnesh_cur IS
6 select PFNUM from employee;
7 BEGIN
8 OPEN ratnesh_cur;
9 FETCH ratnesh_cur into t;
10 FOR i IN 0..LENGTH(t)
11 loop
12 FOR j IN 0..9
13 loop
14 IF SUBSTR('t',i,1)= j THEN dbms_output.put_line(t);
15 ENDIF;
16 END LOOP;
17 END LOOP;
18 CLOSE ratnesh_cur;
19* END
20 /
END LOOP;
ERROR at line 16:
ORA-06550: line 16, column 5:
PLS-00103: Encountered the symbol "LOOP" when expecting one of the following:
if
ORA-06550: line 19, column 3:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the
following:
loopTO APC ...
<quote>Which solution is appropriate depends on the spec details.</quote>
Indeed ⦠that is why I provided a simple alternative answering the original question (my interpretation of it extrapolated from the actual data set provided) ⦠I did not comment on your reply.
But let me address your answers:
<quote>The best way of checking whether a value is numeric is to test for failure of the Oracle built-in function TO_NUMBER()</quote>
That would be too strong a statement, wouldnât it?
Let us assume a table with a varchar2 column which we know contains only alphanumeric characters ⦠2097152 rows ⦠half containing only digits ⦠half with a mix of digits, upper and lower alphabetical characters.
flip@FLOP> select count(0) from apc
2 where instr(translate(lower(v)
3 ,'abcdefghijklmnopqrstuvwxyz'
4 ,'xxxxxxxxxxxxxxxxxxxxxxxxxx'),'x') > 0
5 ;
COUNT(0)
1048576
Elapsed: 00:00:05.02
flip@FLOP> select count(0) from apc
2 where is_number(v) = 0
3 ;
COUNT(0)
1048576
Elapsed: 00:00:13.05
âis_numberâ is your function modified to return 1 (true) or 0 (false) instead of the Boolean.
Clearly a solution employing TO_NUMBER is not always the most performant way (as you seem to imply) ⦠all those context switches between SQL and PL/SQL do add up.
Soooo ⦠there is no âmost performant wayâ to check for numeric or non-numeric values for all categories of problems ⦠in fact there isnât even a universal solution ⦠one has to have some knowledge of the data domain being checked and the environment context.
So how about the â?â and '@â? ⦠Is the string â-1?123@99' numeric or not? Having modified your âis_numberâ function yet again to do âln := to_number(pv_string,'9G999G999D99');â â¦
flip@FLOP> select is_number('-1?123@99') from dual;
IS_NUMBER('-1?123@99')
0
NO.
flip@FLOP> alter session set nls_numeric_characters='@?';
Session altered.
flip@FLOP> select is_number('-1?123@99') from dual;
IS_NUMBER('-1?123@99')
1
YES.
But forget about nls settings ⦠how about â-123e2â? ⦠is this numeric or not ?
flip@FLOP> select to_number('-123e2') from dual;
TO_NUMBER('-123E2')
-12300
According to TO_NUMBER, it is numeric ⦠for the person having the knowledge of the data domain being checked that well may be a false positive.
Hope this proves the point about the universal solution and the qualities of TO_NUMBER.
As for <quote>And if the character is uppercase?</quote> ⦠this kind of flipped me ⦠looking at the link supplied by you ⦠and glancing over the implementation of âStringParseâ one could well ask: âand if the string contains lowercase?â ⦠but nobody did ⦠wouldâve been a bit too picky and outside the main technique being demonstrated.
Gabe -
Need Help using APEX 2.2
Hi All,
Kindly help as i am new to this development tool ( APEX2.2)
I created two item named value1,value2 of textfied type (page item) which is database numberic table columns ,My task is if user enter any numberic values in these two column Values1,value2 then values should insert into table else null means if user will not enter any value.
Thanks in advance.
Regards
gnYou want to lose the '.'s from those regexps:
create or replace function is_number (s in varchar2) return varchar2
is
n number;
begin
n := to_number(s);
return 'Y';
exception
when value_error or invalid_number
then
return 'N';
end is_number;
FUNCTION IS_NUMBER compiled
with tests as (
-- Valid numbers in Oracle
select '-273' s from dual
union all
select '-1' from dual
union all
select '-.01' from dual
union all
select '0' from dual
union all
select '0.00001' from dual
union all
select '.01' from dual
union all
select '+0.1' from dual
union all
select '1' from dual
union all
select '+1' from dual
union all
select '3.141592653' from dual
union all
select '1066' from dual
union all
select '1E6' from dual
union all
select '4459679422440328' from dual
union all
select '6.62606896e-34' from dual
union all
select '6.022e23' from dual
union all
-- Valid number depending on NLS settings
select '1,001' from dual
union all
-- Invalid numbers
select 'XXX' from dual
union all
select '2.O1' from dual
union all
select '1-' from dual
union all
select '1-11' from dual)
select
s
, case
when regexp_like(s, '^[[:digit:]].*$') then 'Y'
else 'N'
end "^[[:digit:]].*$"
, case
when regexp_like(s, '^[[:digit:]]*$') then 'Y'
else 'N'
end "^[[:digit:]]*$"
, is_number(s) "is_number"
from
tests
S ^[[:digit:]].*$ ^[[:digit:]]*$ is_number
-273 N N Y
-1 N N Y
-.01 N N Y
0 Y Y Y
0.00001 Y N Y
.01 N N Y
+0.1 N N Y
1 Y Y Y
+1 N N Y
3.141592653 Y N Y
1066 Y Y Y
1E6 Y N Y
4459679422440328 Y Y Y
6.62606896e-34 Y N Y
6.022e23 Y N Y
1,001 Y N N
XXX N N N
2.O1 Y N N
1- Y N N
1-11 Y N N Agreed that there's a critical difference whether "numberic" means the requirement is for "strings that consist entirely of numeric characters" or "strings that can be converted into values of type NUMBER".
See previous posts:
{message:id=3840440}
{thread:id=982238} -
Question regarding a particular query
hi all
i need ur help to write a query. please cosider the following scenario:
There is a table having colums (Attribute, Value) and data:
Attribute Value
a1 1
a2 2
a3 3.2
a4 a1*a2+a3
a5 a3 - a1
is it possible to write a query which will bring:
a1 1
a2 2
a3 3.2
a4 5.2
a5 2.2
Meaning the same column (Value) contains the numeric values as well as the formulas. There can be any number of operands, and all kind of possible operators.
Waiting for a response.
OmerGiven you have following table:
Name
VARIABLE VARCHAR2
FORMEL VARCHAR2
and you implement the following package:
CREATE OR REPLACE PACKAGE EVALUATE_STRING AS
FUNCTION evaluate(v_formel VARCHAR2, level NUMBER:= 0) RETURN VARCHAR2;
END;
CREATE OR REPLACE PACKAGE BODY EVALUATE_STRING AS
FUNCTION evaluate(v_formel VARCHAR2, level NUMBER:= 0) RETURN VARCHAR2 IS
CYCLIC_ERROR EXCEPTION;
UNDEFINED_VARIABLE EXCEPTION;
V_AKT_VAR VARCHAR2(4000);
V_AKT_CHAR VARCHAR2(1);
V_OPERANDS VARCHAR2(20):= '+-*/()';
V_TODO VARCHAR2(32767);
what VARCHAR2(20):= 'A NUMBER';
V_ERGEBNIS VARCHAR2(4000);
V_FORMEL_WERT VARCHAR2(32767);
v_error VARCHAR2(255);
FUNCTION IS_NUMBER(v_string VARCHAR2) RETURN BOOLEAN IS
laenge NUMBER;
help VARCHAR2(32767);
anz NUMBER;
erg BOOLEAN:= TRUE;
BEGIN
laenge:= length(v_string);
anz:= 1;
WHILE erg AND (anz<=laenge) LOOP
IF instr('1234567890.',substr(v_string,anz,1))!= 0 THEN
anz:= anz+1;
ELSE
erg:= FALSE;
END IF;
END LOOP;
RETURN erg;
EXCEPTION
WHEN OTHERS THEN RETURN FALSE;
END IS_NUMBER;
FUNCTION IS_FORMEL(v_string VARCHAR2) RETURN BOOLEAN IS
laenge NUMBER;
help VARCHAR2(32767);
anz NUMBER;
erg BOOLEAN:= FALSE;
BEGIN
laenge:= length(v_string);
anz:= 1;
WHILE NOT erg AND (anz<=laenge) LOOP
IF instr(V_OPERANDS,substr(v_string,anz,1)) = 0 THEN
anz:= anz+1;
ELSE
erg:= TRUE;
END IF;
END LOOP;
RETURN erg;
EXCEPTION
WHEN OTHERS THEN RETURN FALSE;
END IS_FORMEL;
BEGIN
IF level >= 40 THEN
raise CYCLIC_ERROR;
END IF;
IF NOT IS_NUMBER(v_formel) THEN
FOR x IN 1 .. NVL(LENGTH(v_formel),0) LOOP
V_AKT_CHAR:= substr(v_formel,x,1);
IF instr(V_OPERANDS,V_AKT_CHAR) != 0 OR V_AKT_CHAR = ' ' OR x = LENGTH(v_Formel) THEN
IF x = LENGTH(v_Formel) THEN
V_AKT_VAR:= V_AKT_VAR||V_AKT_CHAR;
END IF;
IF V_AKT_VAR IS NOT NULL THEN
IF IS_NUMBER(V_AKT_VAR) THEN
V_TODO:= V_TODO||V_AKT_VAR;
ELSE
IF IS_FORMEL(V_AKT_VAR) THEN
V_TODO:= V_TODO||evaluate(V_AKT_VAR,level+1);
ELSE -- a Variable
begin
SELECT FORMEL
INTO V_FORMEL_WERT
FROM FORMULAR
WHERE VARIABLE = V_AKT_VAR;
exception
when others then
RAISE UNDEFINED_VARIABLE;
end;
V_TODO:= V_TODO||evaluate(V_FORMEL_WERT,level+1);
END IF; /* if is_formel(..*/
END IF; /* if is number */
END IF; /* if v_akt_var is not null */
V_AKT_VAR:= NULL;
IF instr(V_OPERANDS,V_AKT_CHAR) != 0 THEN
V_TODO:= V_TODO||V_AKT_CHAR;
END IF;
ELSE
V_AKT_VAR:= V_AKT_VAR||V_AKT_CHAR;
END IF; /* if instr(V_OPERANDS,V_AKT_CHAR) .. */
END LOOP;
ELSE
V_TODO:= v_formel;
END IF;
EXECUTE IMMEDIATE 'SELECT '||V_TODO||' FROM DUAL' INTO V_ERGEBNIS;
RETURN V_ERGEBNIS;
EXCEPTION
WHEN CYCLIC_ERROR THEN
RAISE_APPLICATION_ERROR (-20000, 'Error:Max. level reached!');
WHEN UNDEFINED_VARIABLE THEN
RAISE_APPLICATION_ERROR (-20000, 'Undefinded Variable: '||V_AKT_VAR);
WHEN OTHERS THEN
v_error:= SQLERRM;
RAISE_APPLICATION_ERROR (-20000,v_error||' '||V_TODO);
END evaluate;
END;
you are able to select:
SQL> select substr(variable,1,10) VAR,
2 substr(formel,1,20) FORMEL,
3 substr(evaluate_string.evaluate(formel,0),1,30) erg
4 from formular;
VAR FORMEL ERG
a1 1 1
a2 2 2
a3 3.2 3.2
a4 a1*a2+a3 5.2
a5 a3 - a1 2.2
a6 (5-3)*2 4
a7 3 * 3 9
Regards
Anna -
Use IS_NUMBER to test data before using one of the numeric conversion functions, IIF( IS_NUMBER ( ITEM_PRICE ), TO_FLOAT( ITEM_PRICE ), 0.00 )ITEM_PRICE RETURN VALUE '123.00' 123 '-3.45e+3' -3450 '3.45E-3' 0.00345 ' ' 0.00 Consists entirely of blanks '' 0.00 Empty string '+123abc' 0.00 '' 123ABC' 0.00 'ABC' 0.00 '-ABC' 0.00 NULL NULL Hpoe this helps! Regards,Veeru
Hello All, I have got a scenario to implement: I have a Character source column which I need to convert to Numeric(18,4) and load into table. My source and target is a Netezza table. If the conversion fails, we do not drop the record, instead insert NULL for those records,i.e. source and target has same number of records. I use an expression as below : DECODE(Input_String, NULL, NULL-- Check for alphabets IIF((UPPER(Input_String) != LOWER(Input_String)), NULL-- Check for special charactersIIF((INSTR(Input_String,',',1) > 0 || INSTR(Input_String,':',1) > 0 || INSTR(Input_String,'@',1) > 0),NULL, TO_DECIMAL(Input_String,4)) ) ) Now my question is, can we use a single INSTR statement and check for all the special characters? I want to avoid writing multiple INSTR statements. Please suggest me how to check multiple characters in a single INSTR statement. Thanks & RegardsBiswajeet.
-
Fax function of HP LaserJet Enterprise 500 Color MFP M575
Hi,
I am one of users of HP LaserJet Enterprise 500 Color MFP M575.
I would like to know can I set the printer do not print my fax number and fax header on paper when I fax my document to others.
I had login as admin but I did not see where I can config it.
Thank you.
Regards,
Jimmy PunThere is no change to this problem after 6 months since the last post. Put simply, and as stated in the previous posts, there is no way to scan a document and receive the image on your PC. If you are working with graphics for any length of time or want to create an image for inclusion as an image in a document or web page using this piece of equipment will not allow you to do so.
Great shame as every other function works well - it cannot be beyond the wit of HP to include a Windows application that enables you to scan an image, receive it in the software you are using at the time (e.g. Outlook, Word, Photoshop...) and use it in your work.
HP Printers several years older than this £1000+ Enterprise printer were able to do this simple job and have done so for a great many years. Just being able to use Microsoft's Fax and Scan would be start. -
Logical Operations in SQL decode function ?
Hi,
Is it possible to do Logical Operations in SQL decode function
like
'>'
'<'
'>='
'<='
'<>'
not in
in
not null
is null
eg...
select col1 ,order_by,decode ( col1 , > 10 , 0 , 1)
from tab;
select col1 ,order_by,decode ( col1 , <> 10 , 0 , 1)
from tab;
select col1 ,order_by,decode ( col1 , not in (10,11,12) , 0 , 1)
from tab;
select col1 ,order_by,decode ( col1 ,is null , 0 , 1)
from tab;
Regards,
infan
Edited by: user780731 on Apr 30, 2009 12:07 AM
Edited by: user780731 on Apr 30, 2009 12:07 AM
Edited by: user780731 on Apr 30, 2009 12:08 AM
Edited by: user780731 on Apr 30, 2009 12:08 AM
Edited by: user780731 on Apr 30, 2009 12:09 AMexample:
select col1 ,order_by,case when col1 > 10 then 0 else 1 end
from tab;
select col1 ,order_by,case when col1 <> 10 then 0 else 1 end
from tab;
select col1 ,order_by,case when col1 not in (10,11,12) then 0 else 1 end
from tab;As for testing for null, decode handles that by default anyway so you can have decode or case easily..
select col1 ,order_by,decode (col1, null , 0 , 1)
from tab;
select col1 ,order_by,case when col1 is null then 0 else 1 end
from tab; -
Hi,
When we use IF in calculation view with CE function the SQL engine is used.
When we remove the "IF" the CE engine is used.
Is there any alternative for if or case in CE functions?
Thanks,
AmirIs it possible to use CE_CALC for this functionality?
We are trying to use it inside projection:
res_bal = CE_PROJECTION (:bal,[ "BUDAT", "RYEAR" , "Bal_Date" AS "BALANCE_DATE", "RTCUR" ,"MAX_ZGROUP"],'"BALANCE_DATE" == 20140101');
works but:
res_bal = CE_PROJECTION (:bal,[ "BUDAT", "RYEAR" , "Bal_Date" AS "BALANCE_DATE", "RTCUR" ,"MAX_ZGROUP"], '"BALANCE_DATE" == CE_CALC( 'if(''20140101'' == ''19000101'', ''19000101'', ''20140101'')');
Doesn't work.
Thanks,
Amir -
I record my band rehearsals using the voice memo on my iPhone 5s. Then I sync to iTunes and convert file to mp3. When i sync back to my iPhone it appears in my music. So far, so good. When I use the search function in music it finds the file but won't play from tapping. I must troll through all my music songs (currently 2227 of them) to find it and then play it. Is it something to do with it still being under the genre "voice memos" or what ? Anybody help please. Thanks
iWeb is an application to create webpages with.
It used to be part of the iLife suite of applications.
iWeb has no knowledge of the internal speaker on your iPhone.
You may want to ask in the right forum. -
HP AiO Remote app will not recognize scan and copy function of HP Envy 120
Good morning! HP AiO Remote App is installed on my iPad4 and in the same WiFi network with my HP Envy 120 printer. The printer is recognized by the app and marked with a green led. When I tap on scan or copy in the app menu, it tells me that these functions were available in the app only for printers which provide these features. But HP Envy 120 has both scanner and copier. And last time it worked. Some idea what could have happened here? Thanks. UJ
Replied on: http://h30434.www3.hp.com/t5/ePrint-Print-Apps-Mobile-Printing-and-ePrintCenter/HP-AiO-Remote-will-n...
TwoPointOh
I work on behalf of HP
Please click “Accept as Solution ” if you feel my post solved your issue, it will help others find the solution.
Click the “Kudos, Thumbs Up" on the bottom to say “Thanks” for helping! -
HP AiO Remote will not recognize scan and copy function of HP Envy 120 printer
Good morning! HP AiO Remote App is installed on my iPad4 and in the same WiFi network with my HP Envy 120 printer. The printer is recognized by the app and marked with a green led. When I tap on scan or copy in the app menu, it tells me that these functions were available in the app only for printers which provide these features. But HP Envy 120 has both scanner and copier. And last time it worked. Some idea what could have happened here? Thanks. UJ
Hi UJKarl, welcome to the HP Forums. You should be able to scan from the HP AIO Remote App on your Envy 120 printer. You probably just need to power cycle the printer, iPad and router to regain proper function.
Turn off your printer with the power button. Power down the iPad by holding the sleep button down until you get the option to 'slide to power off'. Then disconnect the power cord to your router and count to about 10, and then plug it back in. Once the router comes back online, turn the printer on. When the printer comes back online (blue wi-fi light stops blinking), then power the iPad back up. Try again, and it should work.
Let me know how it turns out.
TwoPointOh
I work on behalf of HP
Please click “Accept as Solution ” if you feel my post solved your issue, it will help others find the solution.
Click the “Kudos, Thumbs Up" on the bottom to say “Thanks” for helping! -
7515 ios 8 scanning doesn't function using document feeder
My AiO remote updated on my iPad today and i have issues scanning from the scanner. The printer and tablet functions properly when scanning using the glass however when using the document feeder, AiO remote scans the sheets then before saving it says there is a problem with the scanner, check the scanner. not sure why it works with the glass and NOT with the feeder.
I am having the exact same problem. I will add that scanning from the document feeder to my Mac Book works fine. The problem with the document feeder is only whenn atttempting to use the AiO software on my iPad.
-
I want to implement thems functionality in my swing application
Hi All...
I want to implement the themes object in my swing application,How ,where to start and move that functionality,If anybody use this functionality in your program experience can u share with me.
Than to Advance
ARjun...Hi Kuldeep Chitrakar
I can do that in web intelligence. dont want that
I am replicating some of the sample report of SQL Servers in BusinessObjects XI R3.1.
One of the SQL Sever's report is of product catalogue type that gives complete information like name, category, description along with the products image etc... for each of the products.
I am trying to replicate the above said SQL Server report in Business objects XI R3.1. I am facing problem in bringing the image in to my BO report. The image resides in a table and its datatype is of varbinary(max). I don't know the exact matching while creating an object in the uiverse designer.
Here is the url link http://errorsbusinessobjectsxir3.blogspot.com/2010/10/business-objects-image-errors.html
Regards
Prasad
Maybe you are looking for
-
How do you use web inspector with Windows 7
I plug in the USB cable from my Windows desktop to my phone, The drivers are loaded successfully, but where can I find the Windows equal to Safari's advanced preferences.
-
GR against the Outbound Delivery
Hi If I MIGO Goods Receipt referencing the PO the Stock account is posted. But I MIGO Goods Receipt referencing the Outbound Delivery Order the stock account is not get posted. Basically, I am doing GR for the Intercompany STO (using NB P.O type). si
-
Cannot hear audio for subclips playback in source monitor
Hi, My editor has merged a bunch of video and multichannel audio on his computer and created merged clips and sub clips for me to look at. Now it's my turn to make selects from the merged subclips, but when I open the project on my computer the subcl
-
Deleted past calender history in Microsoft Outlook
Hello I have only had my Blackberry for a couple of weeks. I have syncronized the device to Microsoft Outlook on my laptop, when i went to look through some things in my calender from last year i discovered that everyting before January this year has
-
PO Condition value not flowing to GR
Hello Sir, We have a custom PO COndition Type and Account Key for one of the costs on a material. Recently we had few examples, where the value in PO for that condition type is flowing, but the value is not flowing during GR. Any pointers on how i sh