如何检查postgres中的值类型 [英] how to check type of value in postgres

查看:232
本文介绍了如何检查postgres中的值类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想像这样检查postgres中的值类型:

I want to check type of value in postgres like this:

SELECT id,
       CASE 
         WHEN val_is_integer THEN (SOME_QUERY)
         WHEN val_isnot_integer THEN (ANOTHER_QUERY)
         ELSE 0
       END
  FROM test;

该怎么做?

注意:
的值是表中的varchar类型,在该字段中有value值是数字和varchar ...

notes: the value is varchar type in table, and in that field there is value is numeric and varchar ...

示例:

ID | value
1 | test
2 | 10
3 | 12
4 | test123


推荐答案

如果有人想知道如何获取变量的数据类型(不是列),可以使用 pg_typeof(any)函数。

If anyone else wonders How to just get data type of a varible (not column) you can use the pg_typeof(any) function.

简单

SELECT pg_typeof(your_variable);

OR

SELECT pg_typeof('{}'::text[]); //returns text[];

注意

pg_typeof(varchar_column)将返回字符变化,而与
的列内容无关。任何列或变量都已经输入,并且
pg_typeof将返回该声明的类型。根据该列(或变量)的值,它将找不到最佳
拟合类型。 - quote from a_horse_with_no_name的评论。

pg_typeof(varchar_column) will return character varying regardless of the content of the column. Any column or variable is already typed and pg_typeof will return that declared type. It will not find the "best fitting" type depending on the value of that column (or variable). -- quote from a_horse_with_no_name's comment.

这篇关于如何检查postgres中的值类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆