如何打印十进制值

时间:2018-08-03 07:19:52

标签: sql sql-server

select total_hours, total_hour_present,
 (total_hour_present / total_hours) * 100 as total_hour_present 
from attendancereport_subject 
where roll_no = '08ME001'

5 个答案:

答案 0 :(得分:0)

如果结果中缺少小数,则将导致“ total_hour_present”和“ total_hours”均为整数。

因为在SQL Server中,当INT除以INT时,它将返回一个INT。

因此您可以将total_hours强制转换或转换为浮点数或小数。
这样除法返回带十进制值的数字。

示例1

(total_hour_present / cast(total_hours as decimal(10,0))) * 100 as percentage_hour_present 

示例2

(total_hour_present / convert(float, total_hours)) * 100 as percentage_hour_present 

答案 1 :(得分:0)

这是一个猜测,但是如果2小时的列是整数,那么您很可能会得到零,因为结果总是小于1。因此,请尝试执行以下操作:

convert(decimal(10,2),(convert(decimal(10,2),total_hour_present) / total_hours) * 100) as total_hour_present

答案 2 :(得分:0)

下面应该可以工作

select total_hours, total_hour_present,
 ((total_hour_present*1.00) / (total_hours*1.00)) * 100.000 as total_hour_present 
from attendancereport_subject 
where roll_no = '08ME001'

答案 3 :(得分:0)

尝试

select total_hours,
   total_hour_present,
   (cast (total_hour_present as decimal) / total_hours) * 100 as total_hour_present 
from attendancereport_subject 
where roll_no = '08ME001'

答案 4 :(得分:-1)

您需要将值转换为十进制,请尝试以下操作:

 select total_hours, total_hour_present,
    (cast(total_hour_present as decimal(18,0)) / total_hours) * 100 as 
    total_hour_present 
    from attendancereport_subject 
    where roll_no = '08ME001'