|
|
|
|
|
|
Re: how to avoid negative values to number datatype in oracle [message #603029 is a reply to message #603028] |
Wed, 11 December 2013 01:23 |
John Watson
Messages: 8960 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
Littlefoot wrote on Wed, 11 December 2013 07:21I'm afraid I would't agree with that statement. You will after seeing my next two solutions:orclz> select case sign(a) when -1 then -1*a else a end from sam_800;
CASESIGN(A)WHEN-1THEN-1*AELSEAEND
---------------------------------
100
100
100.59
orclz> select sqrt(a*a) from sam_800;
SQRT(A*A)
----------
100
100
100.59
orclz>
|
|
|
Re: how to avoid negative values to number datatype in oracle [message #603030 is a reply to message #603029] |
Wed, 11 December 2013 01:37 |
|
Littlefoot
Messages: 21823 Registered: June 2005 Location: Croatia, Europe
|
Senior Member Account Moderator |
|
|
Not THAT perfect, but - match this!
SQL> WITH test AS (SELECT 100 col FROM DUAL
2 UNION
3 SELECT -200 FROM DUAL
4 UNION
5 SELECT -0.343 FROM DUAL)
6 SELECT col,
7 DECODE (SUBSTR (TO_CHAR (col), 1, 1),
8 '-', to_number(SUBSTR (TO_CHAR (col), 2)),
9 col)
10 result
11 FROM test;
COL RESULT
---------- ----------
-200 200
-,343 ,343
100 100
SQL>
|
|
|
|
|
|
Re: how to avoid negative values to number datatype in oracle [message #603061 is a reply to message #603031] |
Wed, 11 December 2013 04:18 |
martijn
Messages: 286 Registered: December 2006 Location: Netherlands
|
Senior Member |
|
|
okay...I had a few minutes to spare :
SQL> with tbl as
(select 100 a from dual
union all
select -100 from dual
union all
select 0.5 from dual
union all
select -0.5 from dual)
select a, case when exp(a)<1 then -1*a else a end from tbl;
A CASEWHENEXP(A)<1THEN-1*AELSEAEND
---------- --------------------------------
100 100
-100 100
.5 .5
-.5 .5
SQL>
|
|
|
|
Re: how to avoid negative values to number datatype in oracle [message #603069 is a reply to message #603065] |
Wed, 11 December 2013 04:48 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
If it is only to get rid of the "-" sign, then why not LTRIM?
SQL> with tbl as
2 (select 100 a from dual
3 union all
4 select -100 from dual
5 union all
6 select 0.5 from dual
7 union all
8 select -0.5 from dual)
9 SELECT LTRIM(a,'-') FROM tbl;
LTRIM(A,'-')
----------------------------------------
100
100
.5
.5
|
|
|