Check for zero and show 2 deciamls in t-sql

kdeutsch
kdeutsch used Ask the Experts™
on
I am selcting a field that shows the number of months.  So I am dividing by 12 to get the years, but I get a decimal string that I want to consolidate.  So i tried CAST and Round but got whole numbers.  Then I also have problem of dividing by zero.  So how can i check for zero and show only like example below.

For example If Ithe field has 91 months it comes out as
7.583333333333
I would like it to be 7.58

Select Cast(sidstrNBR_MO_ACT_FED_SVC /12 as float) from tblsidpers
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Billing Engineer
Most Valuable Expert 2014
Top Expert 2009
Commented:
this should do:
Select Cast(sidstrNBR_MO_ACT_FED_SVC /12 as decimal(20,2) ) from tblsidpers

Open in new window

Top Expert 2010

Commented:
Assuming that sidstrNBR_MO_ACT_FED_SVC is NOT [tiny][small][big]int...

Select Cast(sidstrNBR_MO_ACT_FED_SVC /12 as numeric(10, 2)) from tblsidpers

If it is [tiny][small][big]int...

Select Cast(sidstrNBR_MO_ACT_FED_SVC /12.0 as numeric(10, 2)) from tblsidpers

Author

Commented:
Wow answers a minute apart.  Thanks for the quick help.

Author

Commented:
Works Great

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial