I have a decimal field (19,4) in teradata and I need it in a specific format:
group separator as the point
decimal character as the comma
only two decimal digits
integer part of the number must be grouped 3 by 3
I have already tried FORMAT, TO_CHAR and CAST functions. I have also tried NLS_NUMERIC_CHARACTERS parameter in those functions. I think I am missing some rationale in TERADATA SQL, I'll appreciate some help.
The idea is very simple:
SELECT some_decimal_field_in_proper_format_described_above
FROM some_table
For CAST/FORMAT the group separator and decimal separator are determined by the SDF "locale" for your system. But if you want something different you can use TO_CHAR with D for the decimal and G for the group separator in the format string and custom values for NUMERIC_CHARACTERS:
TO_CHAR(x,'S999G999G999G999G990D99','NLS_NUMERIC_CHARACTERS = '',.''')
Related
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')
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.
I have a number for example 39.46
and i want it to convert in ,format i.e it should look like 39,46 in SQL
is there any function to convert decimal amount in , separated format ?
There is a NLS setting NLS_NUMERIC_CHARACTERS, where you can set "," as decimal separator and "." to separate thousands. They are typically automatically set when you set your locale. You obviously use an English locale.
See here.
This assumes you have actual numbers in your DB and not strings which look like numbers. For strings you may want to use sting conversion operations as described by Thomas G.
Below I demonstrate how to use the number format and the nls_numeric_characters parameter, both for a numeric input and a string input. Notice the d (or D) in the format model, it says "use whatever the appropriate decimal separator is."
SQL> select to_char(93.23, '999d99', 'nls_numeric_characters='',.''') from dual;
TO_CHAR
-------
93,23
1 row selected.
Elapsed: 00:00:00.14
SQL> select to_char(to_number('93.23'), '999d99', 'nls_numeric_characters='',.''')
from dual;
TO_CHAR
-------
93,23
1 row selected.
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
I have a varchar column that has generally has a decimal value, but some times there is some garbage text characters in that field.
Is it possible to filter in the WHERE clause for rows that sucessfully convert to a decimal value?
I am using sql-server 2005
One way is the ISNUMERIC function:
select * from YourTable where ISNUMERIC(col1) = 1
There's one gotcha: isnumeric returns 1 whenever a string can be converted to any numeric type, including money. For example, say you have rows using varying decimal separators, like 7.9 and 7,9. Both will convert to money, and isnumeric returns 1 for both of them. But only one converts to decimal, depending on the SQL Server language settings.