Want to extract digits before decimal and after decimal as string in spark SQL - sql

I have a number like this 000123.00 (lets call it myvar)
I want to use SPARK SQL to get following two results
000123 (Digits before decimal point)
2 (Count of digits after decimal point)
Note: I want the solution in a single SELECT statement.

You can cast the number to string and use split to get both left and right side of decimal point, and then use char_length function to retrieve the number of digits after decimal point.

Related

How to round up to the 16th decimal place in sqlite

I would like to know how to extend to the 16th decimal place in SQLite e.g
SELECT 799.9/150.0 --Answer rounded up to the 16th decimal place.
SQLite has a function called round which can be used to round floating point numbers to a specified precision:
SQLite round() function rounds a floating-point value t up to a number of digits to the right of the decimal point. If the 2nd argument (rounded digits) is omitted, it is assumed to be 0.
To round a number up to the 16th decimal place, you'd have to modify your query to look like this:
SELECT round(799.9/150.0, 16)
This outputs the number 5.332666666666667.
If you want no decimals at all, you can omit the second parameter:
SELECT round(799.9/150.0)
This will return 5.0

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

SQL Division precision

I have 2 columns which I need to divide sum(cola)/sum(ColB), but I am not getting the desired results since SQL server seems to truncate values after decimal
For eg. I have-
select 281370/1035
is giving 271 using simple division, whereas actual result of division is 271.8550724637681 and I want to display 271.8
I tried
SELECT cast(round(281370/1035,1) as numeric(36,1))
but that results 271.0
In SQL Server, you have to cast the integers to decimal and you could use Round to get desired precision.
SELECT cast(Round(CAST(281370 AS decimal) / CAST(1035 AS decimal),1,1) as decimal(10,1))
The problem is that you given the int number and want a decimal result
try this
select convert(decimal(30,10),281370.0/1035.0)
or
select Round(convert(decimal(30,10),281370.0/1035.0),1,1)
#Stormcloak gives the answer to specifically wanting a single position as a mantissa, however to return an exact answer you could "simply" implicitly change the datatype.
select 281370.0/1035
Returns:
271.855072
In Presto DB:
select (CAST(11 as decimal(8,6))/CAST(7 as decimal(8,6))) as result
result:1.571429
decimal(xp,xs)
xp--> total number of digits(before decimal point+ after decimal
point)
xs--> number of digits after the decimal point
reference: https://prestodb.io/docs/current/functions/decimal.html

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.