cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

SQL select string and turn it into a decimal

maxutil
New Contributor II
select
    col as original,
    col::double as val_double,
    col::float as val_float,
    col::decimal(10,4) as val_decimal,
    to_number(col, '99999.99999') as val_tonum
from int_fx_conversion_rate;

The original value of col is a string such as '1.2345' out to 4 decimal places. When I run this above, all of the casted versions come out rounded to 2 decimal places (1.23). How do I get all the decimals?

1 ACCEPTED SOLUTION

Accepted Solutions

Anonymous
Not applicable

@Chris Chung​ : The issue you're experiencing is likely due to the default rounding behavior in SQL. To preserve all the decimal places, you can modify your query to cast the column as a string. Example: CAST(col AS STRING) AS col_string. Please try this.

View solution in original post

2 REPLIES 2

Anonymous
Not applicable

@Chris Chung​ : The issue you're experiencing is likely due to the default rounding behavior in SQL. To preserve all the decimal places, you can modify your query to cast the column as a string. Example: CAST(col AS STRING) AS col_string. Please try this.

Anonymous
Not applicable

Hi @Chris Chung​ 

Thank you for your question! To assist you better, please take a moment to review the answer and let me know if it best fits your needs.

Please help us select the best solution by clicking on "Select As Best" if it does.

Your feedback will help us ensure that we are providing the best possible service to you.

Thank you!

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.