美文网首页
PostgreSQL中int转换为unsigned int

PostgreSQL中int转换为unsigned int

作者: 长江十三寨总瓢把子 | 来源:发表于2021-02-24 14:03 被阅读0次

PG本身是不支持无符号整型的,可能是因为这个不属于SQL标准的内容,可以做,但没必要~

但是近期开发工具的时候,需要在SQL应用层面,能将int类型转换为unsigned int,因此琢磨了一下,开发了个工具函数,纯SQL实现(plpgsql),使用纯SQL是因为不想牵扯编译或者环境配置之类的事情,不然用C的话就是1行代码的事儿。函数地址:PG小工具

使用效果如下:

postgres=# select hashint4(12345);
 hashint4
-----------
 -78097827
(1 row)

postgres=# select to_uint32(-78097827);
 to_uint32
------------
 4216869469
(1 row)

postgres=# select to_uint32(0);
 to_uint32
-----------
         0
(1 row)

postgres=# select to_uint32(12345);
 to_uint32
-----------
     12345
(1 row)

在GDB里面验证一下结果是不是OK

(gdb) p (uint32)(-78097827)
$1 = 4216869469
(gdb) quit

相关文章

网友评论

      本文标题:PostgreSQL中int转换为unsigned int

      本文链接:https://www.haomeiwen.com/subject/hazjfltx.html