max length of the string in Oracle regexp_substr and regexp_replace function - sql

I try to find what is the maximum length of a text as input to the Oracle regexp functions.
thanks

Related

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

PostgreSQL , to_char function

I have this number 0101110 but i need only 5 digits. for exemple (01110)
i used this
select to_char(trim('0101110')::int,'99999V')
but PG returns #####
if someone can help me , thanks a lot.
How do you have a number that starts with a zero? I suspect your "number" is really being stored as a string.
If so, just use right() to take the rightmost five characters:
select right(col, 5)
If the value is really stored as a number, then convert it to a string first.

Convert integer or decimal number to string

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.

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;