format decimals and comma to numbers retrieved - sql

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

Related

How to convert a currency format (from Excel) into a decimal in SQL?

I have a column in a table called [AMOUNT]. The column contains value uploaded from an excel sheet in the Currency Format (i.e $158.96, ($78.36), $714.98, ($7.73), etc). How do I convert these values to a decimal so that it no longer has the $ sign or the parentheses (but it should still have a negative sign for the parentheses values)?
What I thought would work is below and it worked on a similar problem but the values were slightly different (it didn't have parentheses for the negative values).
CAST(CONVERT(VARCHAR(10), CONVERT(MONEY, [AMOUNT])) AS DECIMAL(14,4) AS AMOUNT_V2
Amount
Amount_V2
$158.96
158.96
($78.36)
-78.36
You need to deal with the parenthesis to result in a negative number - one way would be to use translate, such as
select Cast(Translate('($78.36)','()','- ') as money)
Edit - use nested replace if 2016 or prior
select Cast(Replace(Replace('($158.45)','(','-'),')','') as money)

How to turn numeric value into currency in BigQuery?

I am new to BigQuery and am trying to convert numeric values (from Salesforce) to currency (preferably dollar value).
Very basically, what I have currently is:
SELECT salesforce.Name,
ROUND(salesforce.Amount,2) as Amount
FROM table.salesforce
Which obviously only rounds the value to two decimal places.
Regarding your question about how to convert a numeric value to currency value in BigQuery, I would advise you to use the FORMAT() and CONCAT() built-in functions.
I see that in your question you mentioned you want to round the numeric values to the second decimal place, you can do that using FORMAT(), you can read more about it here. In addition, to use the "$" sign, you can use CONCAT(). Below is an example where I used some dummy data to exemplify what I explained:
WITH
data AS (
SELECT
20.21 AS num
UNION ALL
SELECT
99999999.12 AS num
UNION ALL
SELECT
12345 AS num )
SELECT
CONCAT('$ ',FORMAT("%'.2f", num)) AS new_num
FROM
data
And the output:
Notice that in the FORMAT() function I used "%'.2f", which rounds the number to the second decimal place. You can find more information about the meaning of each letter/number in the expression using the following guide.
As a bonus information, the currency values are formatted in a way that the dot "." is a decimal separator and the comma "," is a grouping separator. You can switch that using regex expressions with REGEX_REPLACE() and REPLACE() functions. If that is the case, just let me know so I can help.
This is the method that I use:
CAST(YourNumber AS STRING FORMAT '$999,999')
With decimal points:
CAST(YourNumber AS STRING FORMAT '$999,999.00')

Converting varchar type variable number without having decimal points

I need to convert varchar2 type value 100.0 into 100 (without decimal points) in Oracle SQL. Can you please help me?I used regexp_substr...but it fails in one situation given below.
SELECT REGEXP_SUBSTR(0.1,'(\d*)') FROM DUAL;---it results in Null but i want zero(0).
Note:- in Orcale sql developer
You want to display number in format You wish it to be displayed, so You should use formating functions. No need for rounding here in my opinion - query below should give You what You want.
SELECT
TO_CHAR(TO_NUMBER('100.1'),'FM9999') as res
FROM
dual
;
More on number formats can be found here.
Try to this....
SELECT TRUNC(TO_NUMBER(100.5)) FROM DUAL;

How to add Leading Zeroes and Decimal Points at the same time in SQL?

I have tried many combinations of the SQL functions; so as to have a 12 digit number including the dot character, including leading zeroes and decimal points.
For example:
for the number 121.22, I want to format it to 000000121.22
or for the number 12.2, I want to format it to 000000012.20
or for the number 100, I want to format it to 000000100.00
I have used the following function; but I lost the decimal points if it's zero.
SELECT RIGHT('000000000000'+ STR(CONVERT(VARCHAR,MYNUMBER),12,2),12);
Any idea on how to solve this problem in Microsoft SQL?
If you're on SQL Server 2012 or later, you can use the format() function.
SELECT FORMAT(121.22, '000000000000.00')
SELECT FORMAT(12.2, '000000000000.00')
000000000121.22
000000000012.20
for ms sql versions not in (2012,2014):
cast(right('000000000',9-len(floor(the_number))) as varchar)
+ cast( cast(the_number as decimal(10,2))as varchar)
for ms sql versions in (2012,2014):
format(the_number ,'000000000000.00')
SELECT padded_id = REPLACE(STR(id, 12), SPACE(1), '0')
Is what I add to use (In SQL server) leading 0's as needed, change the 12 to whatever total number of digits you want it to be.
This allows for non hard coded values, just make sure id or whatever column/param you want to format is set.

SQL - Convert number to decimal

I'm trying to convert a number to a decimal with two decimals places.
SELECT CONVERT(DECIMAL(10,2),12345)
The above would return 12345.00 but I'm trying to achieve 123.45
You need something like that:
SELECT CONVERT(DECIMAL(15,2),12345/100.0)
SELECT CONVERT(DECIMAL(10,2),CAST(12345 as float)/CAST(100 as float))
Correction: The premise is somewhat flawed, as the data type of a literal number without a decimal point is int, not numeric as implied by the question. In that case, you do need to convert the initial value to either numeric or decimal before dividing:
SELECT CONVERT(DECIMAL,12345)/100
or
SELECT CAST(12345 AS DECIMAL)/100
(cast is the SQL standard, so if you ever want to apply this to other databases, it would be the preferred method.)
Alternately, you can just add a decimal point to the divisor, as SQL server will return the more precise data type when doing arithmetic on heterogeneous types:
SELECT 12345/100.0
According to the documentation, the numeric data type is functionally equivalent to the decimal datatype, so there's really no reason to convert between the two. It seems that all you really want to do is divide the value you have by 100:
SELECT 12345/100