---
title: "postgresql 除数为0  by zero 异常终极解决办法"
author: "Luob."
date: "2020-04-29T11:29:12Z"
tags: ["PostgreSQL"]
categories: ["数据库"]
slug: "postgresql-0-by-zero"
draft: false
lastmod: "2020-04-29T11:41:34Z"
id: 2513914
url: "https://takeme.iteye.com/blog/2513914"
---

```sql

1. 采用 case 0 when  0 then 1 else  0 end ;
           case null when 0 then 1 else 0 end ;  //报错 说什么 text== inter 操作不存在
         此方法不完美
2. 采用 自定函数  三目运算符   

3. 终极解决办法   兼容 null  和 0   可以试试 

终极1 
select  COALESCE(NULLIF(null,0),1);  

select  COALESCE(NULLIF(0,0),1);  


终极2  这个最好  
select GREATEST(0,1)
select GREATEST(null,1)

```