94

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;

How to do that?


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

example:

ID | value
1 | test
2 | 10
3 | 12
4 | test123
2
  • 4
    The data type has to be the same for all outcomes in a CASE statement that's going to return a column value. But it's not clear what column you're testing, or it's data type... Otherwise you're looking at checking system tables for column data typing & dynamic SQL... Commented Sep 30, 2010 at 4:54
  • Does the PostgreSQL documentation have anything about data type precedence? Commented Sep 30, 2010 at 8:57

3 Answers 3

153

If anyone else wonders How to just get data type of a variable (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.

Sign up to request clarification or add additional context in comments.

2 Comments

@err1 , Could you please put a link to this elsewhere where you found the solution?
@Kajsa: Sorry, it was edited by horse.. but the answer was by selva: stackoverflow.com/questions/20194806/…. Specifically, selva's answer helped me come up twith this: "SELECT data_type FROM information_schema.columns WHERE table_name = '{0}' AND column_name = '{1}'"
14

Your value column is always of type varchar, it seems you want to check if the content is a number/integer.

You could do that by creating a function, e.g.

create function isdigits(text) returns boolean as '
select $1 ~ ''^(-)?[0-9]+$'' as result
' language sql;

(That function could probably be implemented by trying to cast the text to int, or using the int4() function and catching the error that occurs too, and return NULL.)

With such a function you could do:

SELECT id,
       CASE 
         WHEN value IS NULL THEN 0
         WHEN isdigits(value) THEN (SOME_QUERY)
         ELSE (ANOTHER_QUERY)
       END
  FROM test;

Comments

0

For a postgres DB, you can get all the types and their enum values using this query -

SELECT t.typname AS enum_name,e.enumlabel AS enum_value
FROM pg_type t
JOIN pg_enum e ON t.oid = e.enumtypid
JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
WHERE t.typtype = 'e'
ORDER BY enum_name;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.