how to check type of value in postgres

后端 未结 2 397
没有蜡笔的小新
没有蜡笔的小新 2020-12-23 12:58

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 T         


        
2条回答
  •  再見小時候
    2020-12-23 13:41

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

    Simply

    SELECT pg_typeof(your_variable);
    

    OR

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

    Note

    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.

提交回复
热议问题