SQL-Redondeo a 2 decimales


Necesito convertir minutos en horas, redondeado a 2 decimales.También necesito mostrar solo hasta 2 números después del punto decimal. Así que si tengo minutos como 650.Entonces las horas deben ser 10.83

Esto es lo que tengo hasta ahora:

Select round(Minutes/60.0,2) from ....

Pero en este caso, si mis minutos son, digamos,630 horas son 10.5000000. Pero lo quiero como 10.50 solamente (después del redondeo). ¿Cómo logro esto?

 171
Author: Michał Powaga, 2012-04-30

12 answers

¿No podrías lanzar tu resultado como numeric(x,2)? Donde x <= 38

select 
    round(630/60.0,2), 
    cast(round(630/60.0,2) as numeric(36,2))

Devuelve

10.500000   10.50
 296
Author: u07ch,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-05-24 12:23:20

Al igual que con SQL Server 2012, puede usar la función de formato incorporada :

SELECT FORMAT(Minutes/60.0, 'N2')

(solo para lecturas adicionales...)

 59
Author: Matten,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-30 07:55:49

Puede usar

select cast((630/60.0) as  decimal(16,2))
 16
Author: Ram,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-08-23 12:15:37
Declare @number float = 35.44987665;
Select round(@number,2) 
 8
Author: Abhishek Jaiswal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-04 08:46:57

La siguiente consulta es útil y simple-

declare @floatExchRate float;
set @floatExchRate=(select convert(decimal(10, 2), 0.2548712))
select  @floatExchRate

Da salida como 0.25.

 2
Author: ShaileshDev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-30 08:48:00
DECLARE @porcentaje FLOAT

SET @porcentaje = (CONVERT(DECIMAL,ABS(8700)) * 100) / CONVERT(DECIMAL,ABS(37020))

SELECT @porcentaje
 2
Author: Anastacio Valdez,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-30 18:40:06

Funciona tanto con postgresql como con Oracle

SELECT ename, sal, round(((sal * .15 + comm) /12),2) 
FROM emp where job = 'SALESMAN' 
 1
Author: manas mukherjee,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-12-13 17:11:55

El siguiente fragmento puede ayudarte:

select SUBSTR(ENDDTTM,1, 9), extract(DAY FROM (ENDDTTM)), ENDDTTM, BEGINDTTM,  (ENDDTTM - BEGINDTTM),substr(BEGINDTTM, 1,15), substr((ENDDTTM - BEGINDTTM), 12, 8),
round((substr((ENDDTTM - BEGINDTTM), 12, 2)* 3600 + substr((ENDDTTM - BEGINDTTM), 15, 2)*60 +  substr((ENDDTTM - BEGINDTTM), 18, 2)),2) as seconds,
round((substr((ENDDTTM - BEGINDTTM), 12, 2)* 60 + substr((ENDDTTM - BEGINDTTM), 15, 2) +  substr((ENDDTTM - BEGINDTTM), 18, 2)/60 ), 2)as minutes,
round((substr((ENDDTTM - BEGINDTTM), 12, 2) + substr((ENDDTTM - BEGINDTTM), 15, 2)/60 +  substr((ENDDTTM - BEGINDTTM), 18, 2)/3600 ),2)  as hours
 0
Author: Venkat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-10-06 18:31:58

Lo que se use en denominación debe ser en decimal, por ejemplo {[1] } dará 15.00

Si reemplazamos 100 con 100.0 en nuestro ejemplo, obtendremos 15.48

select 1548/100 
15.00000

select 1548/100.0
15.4800

0
 0
Author: nitin157,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-10 20:30:01

Prueba esto: SELECT CAST(ROUND([Amount 1]/60,2) AS DECIMAL(10,2)) as TOTAL

 0
Author: Quintin moodley,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-01 13:44:23

Convierte tu número a Numeric o Decimal.

Reemplace su consulta con lo siguiente.

Sql server

Select Convert(Numeric(38, 2), Minutes/60.0) from ....

MySql:

Select Convert(Minutes/60.0, Decimal(65, 2)) from ....

La función Cast es una envoltura para la función Convert. Si combinamos esto con SQL siendo un lenguaje interpretado, el resultado es que aunque las dos funciones producen los mismos resultados, hay algo más detrás de escena en la función Cast. Usar la función Convert es un pequeño ahorro, pero los pequeños ahorros se multiplican.

 0
Author: Knickerless-Noggins,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-02 07:28:25

Encuentro la función STR el medio más limpio de lograr esto.

SELECT STR(ceiling(123.415432875), 6, 2)
 -1
Author: idro2k,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-08 08:29:32