Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Dec 13, 2012 03:26 PM by StrangerMike
Contributor
2708 Points
1733 Posts
Dec 12, 2012 05:39 PM|LINK
Hello,
I am calculating a percentage and I would like the result to round up to nearest whole number:
This is the line I use for calculating the percentage:
Cast(Taba.Vistor_Cnt - tabb.Vistor_Cnt as numeric (6,0)) / cast(taba.Vistor_Cnt as numeric (6,0)) * 100 as Pct
The results come back as: 25.0000000 or -166.0000000 or 10.2230000.
I would like results to look like 25, -166, or 10 (or perhaps 10.2) not decided if we need to carry the decimcal.
Can you help. Thanks
Participant
1278 Points
195 Posts
Dec 12, 2012 05:49 PM|LINK
Try:
Cast( Cast(Taba.Vistor_Cnt - tabb.Vistor_Cnt as numeric (6,0)) / cast(taba.Vistor_Cnt as numeric (6,0)) * 100 as numeric (6,0)) as Pct
or
Cast( Cast(Taba.Vistor_Cnt - tabb.Vistor_Cnt as numeric (6,0)) / cast(taba.Vistor_Cnt as numeric (6,0)) * 100 as numeric (6,1)) as Pct
Hope this helps.
Dec 12, 2012 06:53 PM|LINK
Thanks, niether compiled clean. Errors:
Incorrect syntax near the keyword 'as'.
Dec 12, 2012 07:05 PM|LINK
Try the following script in SQL Server Management Studio:
declare @ValA numeric(6, 0); declare @ValB numeric(6, 0); set @ValA = 12; set @ValB = 7; select CAST( Cast(@ValA - @ValB as numeric (6,0)) / cast(@ValA as numeric (6,0)) * 100 as numeric (6, 1)) as Pct
All-Star
31515 Points
6433 Posts
Dec 13, 2012 12:00 AM|LINK
convert(int,Cast(Taba.Vistor_Cnt - tabb.Vistor_Cnt as numeric (6,0)) / cast(taba.Vistor_Cnt as numeric (6,0)) * 100) as Pct
convert(decimal(10,1),Cast(Taba.Vistor_Cnt - tabb.Vistor_Cnt as numeric (6,0)) / cast(taba.Vistor_Cnt as numeric (6,0)) * 100) as Pct
Member
271 Points
76 Posts
Dec 13, 2012 01:14 AM|LINK
convert(int,convert(numeric(6,0),Taba.Vistor_Cnt - tabb.Vistor_Cnt ) / convert(numeric(6,0),taba.Vistor_Cnt ) * 100) as Pct
This will work.
Dec 13, 2012 01:37 AM|LINK
convert(int,(Taba.Vistor_Cnt - tabb.Vistor_Cnt / taba.Vistor_Cnt) * 100) as Pctor
Dec 13, 2012 03:10 PM|LINK
Thanks:
Dec 13, 2012 03:12 PM|LINK
thanks
Dec 13, 2012 03:13 PM|LINK
StrangerMike
Contributor
2708 Points
1733 Posts
Help with Round function
Dec 12, 2012 05:39 PM|LINK
Hello,
I am calculating a percentage and I would like the result to round up to nearest whole number:
This is the line I use for calculating the percentage:
Cast(Taba.Vistor_Cnt - tabb.Vistor_Cnt as numeric (6,0)) / cast(taba.Vistor_Cnt as numeric (6,0)) * 100 as Pct
The results come back as: 25.0000000 or -166.0000000 or 10.2230000.
I would like results to look like 25, -166, or 10 (or perhaps 10.2) not decided if we need to carry the decimcal.
Can you help. Thanks
imobsuz
Participant
1278 Points
195 Posts
Re: Help with Round function
Dec 12, 2012 05:49 PM|LINK
Try:
Cast( Cast(Taba.Vistor_Cnt - tabb.Vistor_Cnt as numeric (6,0)) / cast(taba.Vistor_Cnt as numeric (6,0)) * 100 as numeric (6,0)) as Pctor
Cast( Cast(Taba.Vistor_Cnt - tabb.Vistor_Cnt as numeric (6,0)) / cast(taba.Vistor_Cnt as numeric (6,0)) * 100 as numeric (6,1)) as PctHope this helps.
StrangerMike
Contributor
2708 Points
1733 Posts
Re: Help with Round function
Dec 12, 2012 06:53 PM|LINK
Thanks, niether compiled clean. Errors:
Incorrect syntax near the keyword 'as'.
imobsuz
Participant
1278 Points
195 Posts
Re: Help with Round function
Dec 12, 2012 07:05 PM|LINK
Try the following script in SQL Server Management Studio:
declare @ValA numeric(6, 0); declare @ValB numeric(6, 0); set @ValA = 12; set @ValB = 7; select CAST( Cast(@ValA - @ValB as numeric (6,0)) / cast(@ValA as numeric (6,0)) * 100 as numeric (6, 1)) as PctHope this helps.
oned_gk
All-Star
31515 Points
6433 Posts
Re: Help with Round function
Dec 13, 2012 12:00 AM|LINK
or
pratiksolank...
Member
271 Points
76 Posts
Re: Help with Round function
Dec 13, 2012 01:14 AM|LINK
oned_gk
All-Star
31515 Points
6433 Posts
Re: Help with Round function
Dec 13, 2012 01:37 AM|LINK
StrangerMike
Contributor
2708 Points
1733 Posts
Re: Help with Round function
Dec 13, 2012 03:10 PM|LINK
Thanks:
StrangerMike
Contributor
2708 Points
1733 Posts
Re: Help with Round function
Dec 13, 2012 03:12 PM|LINK
thanks
StrangerMike
Contributor
2708 Points
1733 Posts
Re: Help with Round function
Dec 13, 2012 03:13 PM|LINK
thanks