Skip to content

Instantly share code, notes, and snippets.

@franckverrot
Created June 30, 2015 16:39
Show Gist options
  • Save franckverrot/96b5d2225c893c1566a8 to your computer and use it in GitHub Desktop.
Save franckverrot/96b5d2225c893c1566a8 to your computer and use it in GitHub Desktop.
USING statement in PG's ALTER COLUMN
# Create a table
[email protected]/19678=# create table ary(foo text); insert into ary values ('hello world'),('foo bar baz'); select * from ary;
CREATE TABLE
INSERT 0 2
foo
-------------
hello world
foo bar baz
(2 rows)
# Use `USING string_to_array` to separate each word
[email protected]/19678=# alter table ary alter column foo type text[] using string_to_array(foo,' '); select * from ary;
ALTER TABLE
foo
---------------
{hello,world}
{foo,bar,baz}
(2 rows)
# Find out first words and lengths of arrays
[email protected]/19678=# select foo[1], array_length(foo, 1) from ary;
foo | array_length
-------+--------------
hello | 2
foo | 3
(2 rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment