Find a Negative Number in the string [message #552188] |
Tue, 24 April 2012 04:03  |
 |
sulabhagra
Messages: 57 Registered: April 2012
|
Member |
|
|
Hello,
I am currently working on a Data Dictionary project where we need to run a few rules against the give data sources to see if they all comply together.
One of the rule is to check if the no. is negative or not. So for that what I tried to do was to check if first the field is number or not and then check on if it is negative or not.
This is the code I am currently trying on and is not looking good. Can you please help in identifying what is the better way to approach the same.
SELECT 1 FROM DUAL
WHERE decode(DECODE( TRANSLATE('-123.45','-0.123456789',''), NULL, 1,0), 1,substr('-123.45',1,1) ,' ' ) = '-'
Thanks,
S
|
|
|
|
Re: Find a Negative Number in the string [message #552192 is a reply to message #552188] |
Tue, 24 April 2012 04:29   |
 |
Michel Cadot
Messages: 68767 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
Or if you want to play with regular expressions:
SQL> with
2 data as (
3 select '-123.45' v from dual
4 union all
5 select '123.45' v from dual
6 union all
7 select 'michel' v from dual
8 union all
9 select '-123' v from dual
10 union all
11 select '123' v from dual
12 )
13 select v,
14 case
15 when regexp_like(v, '^[+-]{0,1}\d+\.{0,1}\d*$') -- is a number
16 and substr(v,1,1) = '-'
17 then 'Negative number'
18 else 'Other stuff'
19 end
20 from data
21 /
V CASEWHENREGEXP_
------- ---------------
-123.45 Negative number
123.45 Other stuff
michel Other stuff
-123 Negative number
123 Other stuff
Regards
Michel
[Updated on: Tue, 24 April 2012 04:30] Report message to a moderator
|
|
|
|
Re: Find a Negative Number in the string [message #552203 is a reply to message #552188] |
Tue, 24 April 2012 05:25   |
John Watson
Messages: 8988 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
Quote:check if first the field is number or not Does this mean that you may need to determine the data type? The only way I know to do that is with the DUMP function:orcl> select dump(1) from dual;
DUMP(1)
------------------
Typ=2 Len=2: 193,2
orcl> select dump('1') from dual;
DUMP('1')
----------------
Typ=96 Len=1: 49
orcl> There is a table somewhere in the docs that gives the meanings of the Typ values.
|
|
|
|
|
Re: Find a Negative Number in the string [message #552246 is a reply to message #552245] |
Tue, 24 April 2012 09:24   |
 |
sulabhagra
Messages: 57 Registered: April 2012
|
Member |
|
|
Hello,
I worked on this piece and it seems to be OK now... do you agree with it? I tried Mod but that was not working properly
with
data as (
select '-123.45' v from dual
union all
select '123.45' v from dual
union all
select 'michel' v from dual
union all
select '-123' v from dual
union all
select '123' v from dual
union all
select '0' v from dual
union all
select '000.00' v from dual
)
select v,
case
when regexp_like(v, '^[+-]{0,1}\d+\.{0,1}\d*$') -- is a number
and v/1 = 0
then '0'
when regexp_like(v, '^[+-]{0,1}\d+\.{0,1}\d*$') -- is a number
and substr(v,1,1) = '-'
then 'Negative Number'
else 'Other stuff'
end
from data
Thanks
S
|
|
|
|
|
Re: Find a Negative Number in the string [message #552256 is a reply to message #552254] |
Tue, 24 April 2012 10:31   |
_jum
Messages: 577 Registered: February 2008
|
Senior Member |
|
|
I modified the snippet to work "similar" to the function to_number, give NULL in case of not negative number or error and find/fix some other issues, may be it helps:
WITH data AS
(SELECT '-123.45' v FROM DUAL UNION ALL
SELECT ' -12.23 ' FROM DUAL UNION ALL
SELECT ' - 1.2' FROM DUAL UNION ALL
SELECT '+123.45' FROM DUAL UNION ALL
SELECT 'michel' FROM DUAL UNION ALL
SELECT '-123' FROM DUAL UNION ALL
SELECT '--345' FROM DUAL UNION ALL
SELECT '-01' FROM DUAL UNION ALL
SELECT ' 0' FROM DUAL UNION ALL
SELECT ' -0' FROM DUAL UNION ALL
SELECT '-' FROM DUAL UNION ALL
SELECT '-.345' FROM DUAL UNION ALL
SELECT '123' FROM DUAL)
SELECT v,
CASE
WHEN REGEXP_LIKE (trim(v), '^-\d*\.*\d*$')
AND REGEXP_LIKE (trim(v), '^.*[1..9]+.*$')
THEN to_number(trim(v))
ELSE NULL
END numc
FROM data;
v numc
-------------------
-123.45 -123.45
-12.23 -12.23
- 1.2
+123.45
michel
-123 -123
--345
-01 -1
0
-0
-
-.345 -0.345
123
|
|
|
|
|
Re: Find a Negative Number in the string [message #552260 is a reply to message #552259] |
Tue, 24 April 2012 11:15  |
 |
Michel Cadot
Messages: 68767 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
Quote:So the first set checks for then no. in the RE and the other for if it is '0' and both have to satisfy to be TRUE.
Yes.
And as I said, change the second condition.
Regards
Michel
|
|
|