N
N
Nikolay Baranenko2021-10-05 21:27:21
PostgreSQL
Nikolay Baranenko, 2021-10-05 21:27:21

How to do round correctly in postgresql: ERROR: function round(double precision, integer) does not exist?

wrote a request

select
              'request_median_duration' as metric_name,
              EXTRACT(EPOCH FROM request_median_duration)/60 as metric_value,
              TO_CHAR(NOW(), 'YYYY-MM-DD HH:MM:SS') as push_dttm
              from mp_statistics


need to round up to 2 decimal places
tried to do so

select
              'request_median_duration' as metric_name,
              ROUND(EXTRACT(EPOCH FROM request_median_duration)/60,2) as metric_value,
              TO_CHAR(NOW(), 'YYYY-MM-DD HH:MM:SS') as push_dttm
              from mp_statistics


an error is returned

SQL Error [42883]: ERROR: function round(double precision, integer) does not exist
  Подсказка: No function matches the given name and argument types. You might need to add explicit type casts.
  Позиция: 94


got out so

select
              'request_median_duration' as metric_name,
              ROUND(EXTRACT(EPOCH FROM request_median_duration)/60*100)/100 as metric_value,
              TO_CHAR(NOW(), 'YYYY-MM-DD HH:MM:SS') as push_dttm
              from mp_statistics


what is an even more elegant option?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolay Baranenko, 2021-10-06
@drno-reg

found a way to do type conversion via CAST

select
              'request_median_duration' as metric_name,
              round(cast(EXTRACT(EPOCH FROM request_median_duration)/60 as numeric),2) as metric_value,
              TO_CHAR(NOW(), 'YYYY-MM-DD HH:MM:SS') as push_dttm
              from mp_statistics

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question