Other Functions
PG_TYPEOF
Overview
The pg_typeof()
function allows you to retrieve the data type of any given value. It returns a string literal corresponding to the expression type.
Syntax
The syntax of the pg_typeof()
function is:
Here, any represents any value you want to determine the data type.
Example
Case #1: Type of Numeric
This case shows a basic example with a numeric value:
It will return its data type, which is “integer”.
Case #2: Type of String
Here, we will use a string value as an input:
It will return its data type, which is “text”.
Case #3: Type of Interval
Another example is using an interval input:
It will return its data type in string literal.
Case #4: Work with Table
Suppose you have a sample table and want to use pg_typeof()
to retrieve their data types.
- Create the table and insert some data into the table.
- Now, use
pg_typeof()
to determine the data types of the event_time and description columns for each row.
- The result will show that the data type of the event_time column is a timestamp, and the data type of the description column is text: