Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
mufasa
Yak Posting Veteran
62 Posts |
Posted - 2003-10-03 : 09:05:21
|
I would like to know if anyone has a work around divide by zero.I have a view with calculations, which sometimes has a divide by zero error.I know I can use the SET ARITHABORT OFFSET ANSI_WARNINGS OFFTo stop the error, but you cannot use these commands in a view.I use my views as the backbone for my reports, I know I could work around this in Access or Crystal reports, but I would like the SQL server to do most of the work.Any help would be appreciatedThanksMufasa  |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-10-03 : 09:16:28
|
| select col1/nullif(col2, 0) from t |
 |
|
|
mufasa
Yak Posting Veteran
62 Posts |
Posted - 2003-10-03 : 09:27:06
|
quote: Originally posted by Stoad select col1/nullif(col2, 0) from t
Thank-You Very MuchToo bad Books online do not give that exampleMufasa |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2003-10-03 : 10:55:17
|
Um, wouldn't it beselect col1 / case when col2 is null or col2 = 0 then 1 else col2 endSam |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2003-10-03 : 10:56:57
|
Or better - if no result is wanted for divide by zero, then return nullSELECT CASE WHEN col2 IS NULL OR col2 = 0 THEN NULL ELSE col1 / col2 ENDSam |
 |
|
|
|
|
|