环境:pgsql
在使用extract函数获取月份,然后计算季度,作为2个表连接条件的时候,发现查不到数据,后来发现extract函数返回的并不是int类型数据。
具体去查pgsql官方文档,没看到有说extract函数返回的是什么类型,下面举个例子说一下。
select (11 + 2)/3 -- 结果:4
上面的结果是4,可以认为,整除运算,int/int结果也是int。
查季度的时候,用如下sql(当前是11月):
select (extract(month from now()) + 2)/3 -- 结果:4.333333333333333
select extract(month from now())/3 -- 结果:3.6666666666666665
结果是4.333333333333333,所以extract(month from now())不是int类型,相当于numeric类型。
所以上面查季度的,要用cast转为int类型,截断小数部分。
select cast((extract(month from now()) + 2)/3 as int) -- 结果:4
上面说的有什么问题大家可以帮忙指正、补充下。