在SQL Server 2008中,我运行的报表需要划分两列(称为Completes
和Prescreens
,整数),然后我转到Excel并获取{{1除以Prescreens
,然后将其转换为带有2位小数的数字,然后将其转换为百分比。即 - > 0.34,然后变成34%。
在SQL中我知道我可能无法将输出视为“34%”,但至少我希望它为0.34。到目前为止,我有:
Completes
完成数字较小(通常为27,38等),屏幕数字较大(124,276等)
但是这会将我的列输出为全部cast((sq.Completes/sq.Screens ) as float ) as 'conv'
。如何让它显示0.34,0.84等
答案 0 :(得分:6)
试试这样:
select (cast(sq.Completes as float)/cast(sq.Screens as float)) as 'conv'
您也可以执行此操作来舍入值:
select round((cast(sq.Completes as float)/cast(sq.Screens as float)),2) as 'conv'
最后,这将以视觉形式显示为百分比:
select cast(round((cast(sq.Completes as float) /
cast(sq.Screens as float)),2) * 100 as varchar(25))
+ '%' as 'conv'
答案 1 :(得分:2)
纯数字(包括圆形)
cast(100.0*sq.Completes/sq.Screens as decimal(6,0)) conv
百分比
cast(cast(100.0*sq.Completes/sq.Screens as decimal(6,0)) as varchar(6)) + '%' conv