Oracle change string to decimal - oracle

I need to convert string to decimals.
So the string value is: 89,333,22.2345
So i want to keep all decimal places and convert it to: 8933322.2345.
I tried the following query:
select to_number(replace(nvl(89,333,22.2345),0),',','') from dual;
This rounds it to 893322. But i want result with all decimals:
If i try running this query:
select to_number((replace(nvl(89,333,22.2345),0),',',''),'9999.99') from dual;
it throws error.

Try this:
select to_number('89,333,22.2345','99,999,99.9999') from dual;

I think you're passing the number as a parameter. Otherwise why would you use nvl. Then I see the query you need is going to be similar like that
select to_number(replace(nvl('89,333,22.2345','0'),',','')) from dual;
Things were wrong at your code:
since number is a varchar, it has to be placed between apostrophes
nvl takes parameters divided by a comma. And there was one closing bracket too much
nvl(89,333,22.2345),0) -> nvl('89,333,22.2345',0)

Related

How substr works on dates

I am trying to understand a query in my application where it uses substr function.
I have gone through the documentation for substr, which looks simple and clear.
Now below is my query without using substr:
select last_day(to_date(to_char(add_months(TO_DATE('2004/10/25', 'yyyy/mm/dd'),1),'YYYY')||'0201','YYYYMMDD')) from dual;
This gives me result as 2/29/2004. The above query just returns last day of Feb in simple words.
Now I am using substr as below:
select substr(last_day(to_date(to_char(add_months(TO_DATE('2004/10/25', 'yyyy/mm/dd'),1),'YYYY')||'0201','YYYYMMDD')),5,1) from dual;
So here the start value is 5 & length is 1, so I am expecting output as 2 looking at 2/29/2004. but the actual output is E, I am not clear from where this E is coming as result.
You cannot use SUBSTR() on DATE values. SUBSTR() works only on strings!
When you run SUBSTR({DATE_VALUE}, ...) then Oracle actually does following:
SELECT
SUBSTR(
TO_CHAR(
{DATE_VALUE}, (SELECT VALUE FROM nls_session_parameters WHERE parameter = 'NLS_DATE_FORMAT')
), ...
)
FROM dual;
What is the purpose of this query? Do you like to find out whether input year is a leap-year?
Try this instead -
select substr(to_char(last_day(to_date(to_char(add_months(TO_DATE('2004/10/25', 'yyyy/mm/dd'),1),'YYYY')||'0201','YYYYMMDD')),'dd/mm/yyyy'),5,1)
from dual;

Always display decimal at the end of the integer in oracle

I have a requirement where i always need to display the number with the decimal point.
The datatype of the db column is that of number.
If the value is 1.25 it gets displayed as 1.25
But if the value is 1 it does gets displayed as 1 and I need to display the value as 1.00.
I need to perform rpad (right padding) operations once I get the result in the decimal format.
Without the decimal, the value of a whole number would be different from what is present in the database.
Example:
SELECT RPAD(ROUND(12,2), 5 ,0) AS test
FROM DUAL;
results in 12000 whereas I am expecting it to be 12.00.
Any pointers on this would help.
Thanks!
Use an appropriate to_char call. Something like
SELECT to_char( <<your number>>, '0.00' )
FROM dual;
That will return the string "1.00" when you pass in a value of 1. If you pass in a value of 0, it will return the string "0.00". If you pass in a a value of 1.25, it will return the string "1.25".
Try using a number format along with the TO_CHAR function, as:
SELECT TO_CHAR(12, 99.99) AS test
FROM DUAL;
Reference:
You can find documentatation related to other ways to format numbers here.
Try this:
select TRIM(to_char(100.5, '99999999.00')) FROM DUAL
The format specifications are here:
http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#i34570

Oracle Select where NCLOB is Like some string

I have an Oracle table, and in this table I have a column of type NCLOB. I would like to perform a SELECT LIKE on it like so:
SELECT
*
FROM
T_WEB_TASK_IT
WHERE DBMS_LOB.substr( T_WEB_TASK_IT.ISSUE_DESCRIPTION , 32000, 1)
LIKE '%Turning on the%'
But it isn't working, I get an error saying:
String buffer too small
But I don't understand how can that be, cause I know for a fact that there aren't that many characters in that column for that particular record!
You can use DBMS_LOB.INSTR function to search for strings in the lob. Like this:
SELECT *
FROM T_WEB_TASK_IT
WHERE DBMS_LOB.INSTR( T_WEB_TASK_IT.ISSUE_DESCRIPTION , 'Turning on the') > 0
Apart from DBMS_LOB.INSTR, you could also use Regular Expressions:
SELECT *
FROM T_WEB_TASK_IT
WHERE regexp_like(issue_description, 'Turning on the')

Oracle: Pattern for to_char(number) to add additional ascii characters?

Using the Oracle to_char(number) function, is it possible to append ascii characters to the returned string?
Specifically, I need to add a percentage character to the returned string.
"select to_char(89.2244, '999G999G999G999G990D00') from dual" -->
returns "89.22". I need a format pattern that returns "89.22%".
I am using this through reports in Application Express, so cannot simply concatenate "%" to the query, i need to put it in the number format.
So you can't wrap the to_char with a CONCAT?
select concat(to_char(89.2244, '999G999G999G999G990D00'),'%') from dual
You can't do it right in the number format.
If you are able to change NLS_CURRENCY for you session, you can do the following:
SELECT TO_CHAR(1.2, '999G999G999G999G990D00L' /*, 'NLS_CURRENCY=%' */)
FROM dual
---
1,20%
Quick and dirty way:
select to_char(89.2244, '999G999G999G999G990D00L', 'NLS_CURRENCY=''%''') from dual;
SYS # orant11g >select to_char(89.2244, '999G999G999G999G990D00')||'%' from dual;
TO_CHAR(89.2244,'999G999
------------------------
89.22%
Just use the || bars instead of the concat function.

Why is Oracle's to_char() function adding spaces?

Why is Oracle's to_char() function adding spaces?
select length('012'),
length(to_char('012')),
length(to_char('12', '000'))
from dual;
3, 3, 4
The extra leading space is for the potential minus sign. To remove the space you can use FM in the format:
SQL> select to_char(12,'FM000') from dual;
TO_C
----
012
By the way, note that to_char takes a NUMBER argument; to_char('012') is implicitly converted to to_char(to_number('012')) = to_char(12)
To make the answers given more clear:
select '['||to_char(12, '000')||']',
'['||to_char(-12, '000')||']',
'['||to_char(12,'FM000')||']'
from dual
[ 012] [-012] [012]
The format mask that you are using is fixed width and allows for a minus sign
Be aware when using the 'fm' syntax it will not include any values after the decimal place unless specified using zeros. For example:
SELECT TO_CHAR(12345, 'fm99,999.00') FROM dual
returns: '12,345.00'
SELECT TO_CHAR(12345, 'fm99,999.99') FROM dual
returns: '12,345.'
As you can see this would be an issue if you are expecting two zeros after the decimals place (maybe in fee reports for example).

Resources