Skip to content

Instantly share code, notes, and snippets.

@jahio
Created July 24, 2013 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jahio/6073520 to your computer and use it in GitHub Desktop.
Save jahio/6073520 to your computer and use it in GitHub Desktop.
Typecasting data coming out of hstore
testing=# \d foo
Table "public.foo"
Column | Type | Modifiers
--------+--------+-----------
id | uuid | not null
stuff | hstore |
Indexes:
"foo_pkey" PRIMARY KEY, btree (id)
testing=# SELECT * FROM foo;
id | stuff
--------------------------------------+--------------
c1e4833e-96e3-4774-b5bf-39da5c096457 | "foo"=>"bar"
(1 row)
testing=# SELECT CAST(stuff->'foo' AS VARCHAR) FROM foo WHERE stuff->'foo'='bar';
varchar
---------
bar
(1 row)
testing=# SELECT CAST(stuff->'foo' AS UUID) FROM foo WHERE stuff->'foo'='bar';
ERROR: invalid input syntax for uuid: "bar"
@jahio
Copy link
Author

jahio commented Jul 24, 2013

The final SELECT here, converting to a UUID, is just to show that if you try to convert a value to a type that doesn't match, it won't do it. In this case "bar" is the totally wrong format for a UUID (by a huge margin).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment