Re: Accommodating alternative column values

Поиск
Список
Период
Сортировка
От Peter J. Holzer
Тема Re: Accommodating alternative column values
Дата
Msg-id 20240703143209.r3ysarw37hm2mx2d@hjp.at
обсуждение исходный текст
Ответ на Re: Accommodating alternative column values  (Rich Shepard <rshepard@appl-ecosys.com>)
Список pgsql-general
On 2024-07-03 07:13:47 -0700, Rich Shepard wrote:
> On Wed, 3 Jul 2024, David G. Johnston wrote:
> > Yeah, the simply cast suggested will not work. You’d have to apply an
> > expression that turns the current contents into an array. The current
> > contents are not likely to be an array literal.
>
> David,
>
> No, it's not now an array.
>
> I thought that this expression would work, but it doesn't:
> bustrac=# alter table people alter column email set data type varchar(64)[] using email::varchar(64)[];
> RROR:  malformed array literal: "frank@dmipx.com"
> DETAIL:  Array value must start with "{" or dimension information.
>
> If I correctly understand the error detail I'd need to change the contents
> of that column for all 1280 rows to enclose the contents in curly braces
> before I can convert the datatype to an array. Is that correct?

No. You need *some* way of creating an array with a single element which
is your email address. Constructing a valid array literal as a text and
casting that to array type is one way to do this. However, it seems like
a rather cumbersome and error-prone way to me.

As Raymond Hettinger likes to say: "There must be a better way".

And indeed, https://www.postgresql.org/docs/current/functions-array.html
shows lots of array values written as ARRAY[1, 2, 3] or similar. So that
makes it likely that ARRAY[email] creates an array with the intended
contents.

Try it with

    select array[email] from people;

If that looks promising, you can use it in an alter table statement
(Torsten already posted the solution, but I wanted to expand a bit on
how to find it).

        hp

--
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Вложения

В списке pgsql-general по дате отправления:

Предыдущее
От: "David G. Johnston"
Дата:
Сообщение: Re: Accommodating alternative column values
Следующее
От: Adrian Klaver
Дата:
Сообщение: Re: Accommodating alternative column values