杨大伟在路上

pgsql case when 报错: ERROR: invalid input syntax for type numeric ,应该如何处理?

文章目录

问题

      最近有个需求,sql如下:

select 
	case 
		when score < 60  then  60  
		else '优秀' end  
from stuent

     但是运行的时候报错了:ERROR: invalid input syntax for type numeric:'优秀'
      百度说:数据类型不符。
      仔细想一下, 60是int,优秀是string,确实类型不符。
      sql修改如下:

select 
	case 
		when score < 60  then '' || 60  
		else '优秀' end  
from stuent

  这样就都是string了,就不报错了。

把结果扩展一下

      其实这个报错不只case when会报,其他语句也会报这个错。原因都是类型不符,只要抓住这个,去排错就行了。

posted on 2021-10-29 21:01  浪子逆行  阅读(2896)  评论(0编辑  收藏  举报

导航