From b7dfcd4a8b6e75a46fb276d995168e8b5c089f54 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sun, 5 Jul 2015 10:48:22 -0500 Subject: [PATCH] Add Checking The Type Of A Value as a postgres til. --- README.md | 1 + postgres/checking-the-type-of-a-value.md | 41 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 postgres/checking-the-type-of-a-value.md diff --git a/README.md b/README.md index 71a6759..7f02866 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ smart people at [Hashrocket](http://hashrocket.com/). ### postgres - [Auto Expanded Display](postgres/auto-expanded-display.md) +- [Checking The Type Of A Value](postgres/checking-the-type-of-a-value.md) - [Configure The Timezone](postgres/configure-the-timezone.md) - [Count Records By Type](postgres/count-records-by-type.md) - [Default Schema](postgres/default-schema.md) diff --git a/postgres/checking-the-type-of-a-value.md b/postgres/checking-the-type-of-a-value.md new file mode 100644 index 0000000..d95ed59 --- /dev/null +++ b/postgres/checking-the-type-of-a-value.md @@ -0,0 +1,41 @@ +# Checking The Type Of A Value + +The `pg_typeof()` function allows you to determine the data type of anything +in Postgres. + +```sql +> select pg_typeof(1); + pg_typeof +----------- + integer +(1 row) + +> select pg_typeof(true); + pg_typeof +----------- + boolean +(1 row) +``` + +If you try it on an arbitrary string, it is unable to disambiguate which +string type (e.g. `text` vs `varchar`). + +```sql +> select pg_typeof('hello'); + pg_typeof +----------- + unknown +(1 row) +``` + +You just have to be a bit more specific. + +```sql +> select pg_typeof('hello'::varchar); + pg_typeof +------------------- + character varying +(1 row) +``` + +[source](http://www.postgresql.org/docs/9.3/static/functions-info.html#FUNCTIONS-INFO-CATALOG-TABLE)