Reading actual decimal value in Hive - hive

While trying to cast hive decimals as string, only the significant value of the decimal is considered in hive. This is leading to md5 (hash value) mis-matches when the data is migrated. I need the actual decimal value (including trailing 0's).
Eg:-
Query - select cast(7.500000 as string)
Result - 7.5
Expected result - 7.500000

Related

Want to extract digits before decimal and after decimal as string in spark 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.

Find float column max scale and precision

I have a column with datatype float in Teradata. I want to find the Maximum precision and scale for that column.
Note: My column's scale part has more than 10 digits in most of the places.
Sample Data
123.12321323002
13123213.13200003
33232.213123001
The output I need is
Precsion 19 (scale + length of 13123213) and scale is 11 (length of 12321323002)
or
8 (length of 13123213), 11 (length of 12321323002).
I tried to find them buy converting the column as varchar and splitting them based on the '.' and make the integer and fractional part as 2 columns and then finding the max length of 2 columns. But when I'm select the data, Teradata rounds off the scale part. So after that, if I convert them as char, I'm getting lesser value for scale part.
For example:
org data: 1234.12312000123101
data when I select from Teradata: 1234.12312000123
This is a bit long for a comment.
Teradata uses the IEEE format for real/float values. This gives 15-17 digits of precision. Alas, you need 19 digits, so the values you want are not being stored in the database. You cannot recover them.
What you can do is fix the database, so it uses numeric/decimal/number. This supports what you want: NUMERIC(19, 11). You would then need to reload the data so it is correctly stored in the database.
When you need high precision without predefined scale simply switch to the NUMBER datatype, which is a mixture of DECIMAL and FLOAT.
Exact numeric, at least 38 digits precision, no predefined scale, range of 1E-130 .. 1E125.
Float on steroids :-)

How to retrieve the column with mandatory decimal point in Sql 2008?

I am retrieving a numeric value from one of the column in Sql. However I have to retrieve it by three decimal point (eg below).
Though if the value in the table is in the decimal point, then its giving the result as expected. But in case the value is not in decimal format and only an integer it must display .000
Below is the example.
Value in table= 2
Retrieval Value = 2
Required Value = 2.000
As per your requirement convert your incoming value in Decimal(18,3). like given below,
select convert(decimal(18,3),2)
To really show a value in a particular format, you can convert the value to a string. One method uses the str() function:
str(x, 18, 3)
Note that you can also cast() the decimal:
select cast(cast(x as decimal(18, 3)) as varchar(19))

Round a value to two decimal places in SQL

I am trying to round a value in SQL, here is the code that I have:
select round(600.000,2)
How do I get the value 600.00?
Instead of round() convert to a decimal:
select cast(600.000 + 0.5 as decimal(10, 2) )
round() changes the value but it might not change the type of the result. Hence, you might still see extra decimal points (depending on the database and the application). Converting to a decimal with two digits of precision converts both the value and the type.

Select all rows where a varchar column converts to a decimal

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.