Home » SQL & PL/SQL » SQL & PL/SQL » Find a Negative Number in the string (11g)
Find a Negative Number in the string [message #552188] Tue, 24 April 2012 04:03 Go to next message
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 #552189 is a reply to message #552188] Tue, 24 April 2012 04:08 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Clue: if any parameter of TRANSLATE is null (or empty string) then the result is null.

Regards
Michel
Re: Find a Negative Number in the string [message #552192 is a reply to message #552188] Tue, 24 April 2012 04:29 Go to previous messageGo to next message
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 #552202 is a reply to message #552192] Tue, 24 April 2012 05:25 Go to previous messageGo to next message
sulabhagra
Messages: 57
Registered: April 2012
Member
Thanks Michel,

Will look into this, did not use the Reg Exp before, and this looks really cool.

Thanks again,
Sulabh
Re: Find a Negative Number in the string [message #552203 is a reply to message #552188] Tue, 24 April 2012 05:25 Go to previous messageGo to next message
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 #552244 is a reply to message #552202] Tue, 24 April 2012 08:39 Go to previous messageGo to next message
sulabhagra
Messages: 57
Registered: April 2012
Member
Hello Michel,

Can you help me in altering this expression to check if the string is 'Zero'.. as I am not able to alter this at my end.

I mean the rule to check is that the field = 0 and as many of them are string, it is failing in string conversion and erroring out.


Thanks,
S

[Updated on: Tue, 24 April 2012 08:40]

Report message to a moderator

Re: Find a Negative Number in the string [message #552245 is a reply to message #552244] Tue, 24 April 2012 08:41 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Put this specific test in first in CASE expression (if you use the regexp formula) or in your DECODE.

Regards
Michel
Re: Find a Negative Number in the string [message #552246 is a reply to message #552245] Tue, 24 April 2012 09:24 Go to previous messageGo to next message
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 #552253 is a reply to message #552246] Tue, 24 April 2012 10:15 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Instead of "v/1" which implictly converts "v" to a number just use "to_number(v)=0" which is clearer don't you think?

Regards
Michel
Re: Find a Negative Number in the string [message #552254 is a reply to message #552253] Tue, 24 April 2012 10:17 Go to previous messageGo to next message
sulabhagra
Messages: 57
Registered: April 2012
Member
OK, but there is another condition which is checking for 'Number' and this is just an 'AND' to it, so I think we are fine.

Agree?
Re: Find a Negative Number in the string [message #552256 is a reply to message #552254] Tue, 24 April 2012 10:31 Go to previous messageGo to next message
_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 #552257 is a reply to message #552254] Tue, 24 April 2012 10:41 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
sulabhagra wrote on Tue, 24 April 2012 17:17
OK, but there is another condition which is checking for 'Number' and this is just an 'AND' to it, so I think we are fine.

Agree?


I'm not sure I understand your sentence. If you mean it is another 'WHEN', yes and the first part check if it is a number.

Regards
Michel

Re: Find a Negative Number in the string [message #552259 is a reply to message #552257] Tue, 24 April 2012 10:53 Go to previous messageGo to next message
sulabhagra
Messages: 57
Registered: April 2012
Member
No, what I mean is the condition stand as this as the moment:
when  regexp_like(v, '^[+-]{0,1}\d+\.{0,1}\d*$') -- is a number
                 and v/1 = 0


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. Does that makes sense?

Thanks,
S
Re: Find a Negative Number in the string [message #552260 is a reply to message #552259] Tue, 24 April 2012 11:15 Go to previous message
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
Previous Topic: Update Existing Table
Next Topic: Count from 1 table by 2 criteria
Goto Forum:
  


Current Time: Mon Aug 25 04:38:50 CDT 2025