Currency column with no decimals: add in decimal - sql

I've been reviewing a currency column that has no decimal spaces. It's an output from a legacy system loaded into our Oracle database.
If the field has three or more numerals it should have a decimal at three spaces right.
If the value has less than three numerals, it should have a decimal and a leading zero.
For example:
2050 should be converted to 2.050
110 should be converted to .110
50 should be converted to .050
I've tried using cast, but I received the error 'invalid datatype.'
It's a basic select statement:
select
customer_id
cast(ENDING_BALANCE as (decimal(10,3)) as Bal_1
from Current_Balances
Any suggestions would be appreciated.

I think you need to cast it to a number and divide by 1000
SELECT CAST(CAST('2050' as INT)/1000 as DECIMAL(10,3)) FROM DUAL
If you really mean to have the output format looking like that, you need to TO_CHAR it
SELECT LTRIM(TO_CHAR(CAST('2050' as INT)/1000, 'FM0.000'), '0') FROM DUAL

Related

Need dynamic decimal places in SQL display

In a display column I'm getting values as follows:
12.000000
12.350000
13.230000
14.560000
I need to represent these values with dynamic decimal places upto 2 places i.e if there are zero's then it should ignore.
for example: 12.35 only , if its 12.500000 then it should display 12.5 only
float type removes trailing zeros, at least for SQL Server. Try casting your decimal to float.
SELECT CAST(12.350000 as float)
Returns
12,35
You could try the following (ANSI standard SQL):
SELECT TRIM(TRAILING '0' FROM TRIM(CAST(CAST(myvalue AS DECIMAL(12,2)) AS CHAR(30))))
FROM mytable;
I'm not sure what flavor of SQL you're using but the above should work (the extra TRIM() is on the off-chance that the result of the CAST() is padded with spaces.

SQL keep two decimal places for integers after converting cent into dollar

I am trying convert number values stored in the database as cents into dollar and keep 2 decimal places.
The following code will work only if v_cent/100.00 is NOT integer
SELECT CAST(ROUND(v_cent/100.00, 2) AS NUMERIC(8,2)) FROM DUAL;
If v_cent = 20000 then the result is 200.
How could I reserve 2 decimal places even if the result is integer?
Use to_char(), say to convert this to an output format with two decimal places:
select to_char(v_cent / 100.0, 'FM999999.99')
from dual;
As for your formulation it is doing the right thing. The only issue is that the decimal points are not printed out by default.

Want to convert from character format to number format with decimal

want to convert character format (00001000000) as 10000.00. Please help me.
I've already tried with
select to_number('00012300','9999999999.99','nls_numeric_characters = ''.,''') from dual
this script but it can show only 12300, actually i want to view as 123.000
Well, you can convert your value to a number using
select to_number('00012300') from dual;
It seems that you divide by 100.
So
select to_number('00012300') / 100 from dual;
If you want to display two decimals, back to a varchar again, with a format
select to_char(to_number('00012300') / 100, '999999D99') from dual;
What you simply may use, is a workaround like that:
CAST(round(0000000111100 / 100, 2) AS numeric(38, 2))
So what you do is that you round your value by 2 decimal places while dividing by 100 and then casting it to to a numeric with a 2 piece left-over.
If your value is a string, just simply convert it to an integer before.
Peace

Conversion of varchar to number

I have a question that bothers me. How can i convert a varchar to number when inside the varchar value consists of alphabets.
For my varchar price column values:
14dollars10cents
15dollars20cents
By converting it to varchar to number price column, the values should be:
1410
1520
I know that if the varchar does not consists any alphabets, it can auto convert by"
SELECT CONVERT(INT, PRICE) FROM Table
Is there any way to get rid of the alphabets in the middle as I would like to do mathematical function on it.
Updated attempt of putting fixed point number in:
SELECT CAST (Replace(REPLACE(PRICE, 'dollars', '.'),'cents','') AS Number(4,2)))
FROM TEST;
Thanks
You could just use REGEXP_REPLACE to remove all non digit characters:
SELECT REGEXP_REPLACE(price, '[^[:digit:]]')
FROM table;
To then convert this to a number:
SELECT TO_NUMBER(REGEXP_REPLACE(price, '[^[:digit:]]'))
FROM table;
If you want to add the point in then you can do that with REGEXP_REPLACE too:
SELECT TO_NUMBER(REGEXP_REPLACE(val, '^([0-9]*)([^[:digit:]]*)([0-9]*)(.*)$', '\1.\3'))
FROM table;
Voila...
SELECT CAST(REPLACE(YourVarcharCol, 'dollars', '') AS INT) FROM Table
The issue with this is it will break if the varchar still contains alpha-numeric characters.
How about using translate to strip out the unwanted characters.
SELECT TO_NUMBER(TRANSLATE('14dollars10cents','1234567890dolarscents','1234567890')) FROM DUAL
No I don't think there is direct way.
you can do string parsing to get your integer value.

format decimals and comma to numbers retrieved

I have a column in my table which showing an amount. The amount is varying from one column to another and they are more than 15 digits.
What is the best way to format the number to show commas and decimal points?
My query is
select amount from ccamounts
How can I format the number
205511892078
to show as
205,511,892,078
and if there is a radix point it will also appear.
I believe you can use TO_CHAR to do this, the issue is that this is just a formatting function within SQL. It requires that your number is always going to be in the same format.
taking the example above you could do
TO_CHAR('205511892078', '999,999,999,999')
and this would format the number as you have specified, with a decimal place this can be done aswell but the decimal needs to be specified:
TO_CHAR('20551189207842', '999,999,999,999.99')
which would give you 205,511,892,078.42
I think if the field length is going to vary sql will just ignore anything that doesn't fit into the format string (It's a mask). Perhaps you want to consider formatting the number in this case on whichever front end you may be using?
I would format the number in the UI / Reporting tool / Presentation layer not Oracle
but if you MUST format it in oracle try:
SELECT
CASE WHEN INSTR( TO_CHAR(205511892078),'.')>0 THEN
TO_CHAR(205511892078 ,'999,999,999,999.99')
ELSE
TO_CHAR(205511892078 ,'999,999,999,999')
END
FROM DUAL
this will return the number as a string.
declare #d3 decimal (10, 2)
set #d3 = 12309809.5494
SELECT convert(varchar(15),cast(CAST(ROUND(#d3,2,1) AS DECIMAL (30,2)) as money),1) as Value
SELECT CAST(ROUND(convert(varchar(30), cast(#d3 as money),2),2,1) AS DECIMAL (30,2)) as Value
Output:
12,309,809.55
12309809.55