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

gefragt vor 2 Jahren13357 Aufrufe
3 Antworten
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
beantwortet vor 2 Jahren
AWS
EXPERTE
überprüft vor 2 Jahren
0

Suggest to use the round function. See here

profile pictureAWS
EXPERTE
Roi
beantwortet vor 2 Jahren
AWS
EXPERTE
überprüft vor 2 Jahren
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
beantwortet vor 2 Jahren
AWS
EXPERTE
überprüft vor 2 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen