How can I format a decimal (DOUBLE) number in a select statement?

0

Hi, I have the following select statement in Athena and I would like to set the number of digits right to the decimal period of the column value:

SELECT
,(CAST(column1 AS double) / column2) cvr
FROM
  my_table

For example, I current get the following result: 0.6453524

I would like to get it as 0.645

demandé il y a 2 ans13357 vues
3 réponses
1

Hi, it depends on what specifically do you want, with a similar example 0.6456524 and 3 decimals

  • If you want to have a ceiling of the previous decimal value : 0.646 To do this you can use a UDF or this mathematic expression CEIL((CAST(column1 AS double) / column2)*1000)/1000 as cvr
  • if you want to have a floor of the previous decimal value : 0.645 FLOOR((CAST(column1 AS double) / column2)*1000)/1000 as cvr
  • if you want to numerically round the previous decimal value : 0.646. To do this you can use ROUND((CAST(column1 AS double) / column2), 3) as cvr You can always refer to the Athena operations documentation here
AWS
répondu il y a 2 ans
AWS
EXPERT
vérifié il y a 2 ans
0

Suggest to use the round function. See here

profile pictureAWS
EXPERT
Roi
répondu il y a 2 ans
AWS
EXPERT
vérifié il y a 2 ans
0

You can use ROUND(col, 3) to round a number to three decimal points:

SELECT ROUND(CAST(column1 AS DOUBLE) / column2), 3) AS cvr
FROM my_table
AWS
Theo
répondu il y a 2 ans
AWS
EXPERT
vérifié il y a 2 ans

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions