Convert integer or decimal number to string - sql

I need to convert numbers in text format. It could be integers and decimal numbers.
If convert integer without regexp_replace like
select to_char(999,'FM9999.9') from dual;
999.
Solution for this problem is
regexp_replace(to_char(999.9,'FM9999.9'),'[.]$')
Are there any other way to do it without regexp_replace ?

You can use the trim or rtrim functions:
select to_char(999,'FM9999.9') as plain,
rtrim(to_char(999,'FM9999.9'),'.') as trimmed
from dual;
PLAIN TRIMMED
------- -------
999. 999
Documentation for the rtrim function.
You can't do it within the to_char() call though, if that is what you were hoping for; there isn't a format model element that makes the decimal separator optional depending on whether there are decimal digits.

Related

Postgresql decimal point using to_char

I want make a select that adds decimal point into integers, but when I do it, it shows me bunch of # instead of those numbers.
SELECT to_char(1234, '99.99');
What I expected was table with a value of 12.34 but I got ##.## in my select.
However, if I did
SELECT to_char(1234, '99,99');
it showed be 12,34 as expected. Problem is, that I want to have a decimal point and not a comma.
I am using PostgreSQL 13.2
It seems you want to take the last 2 digits and pretend they were decimals. You can't use the predefined . or D formats because they apply to true decimals.
Instead, you can print the dot character (like any other string), between double quotes, before the last 2 digits:
SELECT to_char(1234, '999"."99');
to_char
---------
12.34
PS: on a side note, you are getting the masked output in your 1st query because there isn't enough digit positions on the format:
SELECT to_char(1234, '9999.99');
to_char
----------
1234.00
Welcome to SO. You were very close:) Which locale are you using? Check this example for en_US.UTF-8:
SELECT to_char(1234, '999G99');
to_char
---------
12.34
(1 row)
G: Group separator that uses locale
D: Decimal point that uses locale
Check this tutorial

Teradata SQL format a decimal field

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 = '',.''')

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')

cast a hex value into a char oracle

In teradata we have the concept of casting a hex into a char like the following:
select cast(X'0000' AS CHAR(16)) from something;
What's the equivalent Oracle representation of this X''
Could it be UNISTR?
To convert a series of hexadecimal digits to a number you can use the TO_NUMBER function with the 'X' mask character, as in:
SELECT TO_NUMBER('12AB', 'XXXX') FROM dual;
This produces the (decimal) result 4779.
If you want to go the other way, i.e. convert a number to its hexadecimal representation, you can use the TO_CHAR function:
SELECT TO_CHAR(4779, 'XXXXXXXXXXXXXXXX') FROM dual;
which produces the result ' 12AB'. Note that because the TO_CHAR function leaves room for a sign (+ or -) the returned string is actually 17 characters wide.

to_number function in sql

i could not understand why following code
SQL>
Select to_number('1234.64', '9999.9') from Dual;
returns this number 1234.6?is it something like rounding ,truncation or?please help me to understand this code,i know to_number functions,i have used many times this code for simple chars,but here it is not clear anything
This looks a lot like Oracle, but I suspect that the result would be similar in any SQL that used to_number.
The to_number function takes two arguments: the string to be converted to a number, and the format string for the conversion.
In the example, '12345.64' is the string to be converted, while '9999.9' is the format string. In this format string, a 9 stands for a digit while a . stands for the decimal point.
So the function is asking to convert the string '12345.64' to a number with up to 4 digits to the right of the decimal point, and only 1 digit after the decimal point.
The second argument is optional - under normal circumstances, I would omit it.
You should use
SELECT to_number('1234.64', '9999.99') from Dual;
Your mask tells engine you want just one decimal, so number gets rounded.
If you want to get exact number, don't specify any mask:
SELECT to_number('1234.64') from Dual;