Link to home
Start Free TrialLog in
Avatar of kdeutsch
kdeutschFlag for United States of America

asked on

Check for zero and show 2 deciamls in t-sql

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
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of kdeutsch

ASKER

Wow answers a minute apart.  Thanks for the quick help.
Works Great