Re: Varchar concatenate fields as Char or Varchar, not

From: Scott Marlowe <smarlowe(at)g2switchworks(dot)com>
To: MargaretGillon(at)chromalloy(dot)com
Cc: "Uwe C(dot) Schroeder" <uwe(at)oss4u(dot)com>, pgsql general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Varchar concatenate fields as Char or Varchar, not
Date: 2006-08-07 19:18:17
Message-ID: 1154978296.20252.27.camel@state.g2switchworks.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, 2006-08-07 at 14:11, MargaretGillon(at)chromalloy(dot)com wrote:
> >"Uwe C. Schroeder" <uwe(at)oss4u(dot)com> wrote on 08/07/2006 11:30:28 AM:
> > Well, you cast all those fields to be concatenated to text. Why
> should the db
> > make a varchar out of that? I seriously doubt that 7.x made a
> varchar of that
> > - but then, 7.2 is very very old.
> > So either cast your fields to varchar (i.e. c.refullname::varchar ||
> > d.enname::varchar) or cast the result of the concatenation to a
> varchar.
> >
> > UC
>
> FYI in 8.1.4 if the cast is changed to varchar the result still comes
> out as a text field
> >> SELECT (c.refullname::varchar || d.enname::varchar ||
> f.evname::varchar) AS evlinkname1,
> results in evlinkname1 as text

Just FYI, text and varchar are, internally, pretty much the same types.
varchar has an optional precision setting as in varchar(200) while text
does not allow one.

All the text ops are written for text types, so varchar gets cast as
text (as do char types) before being operated on.

Note that you can cast the result of that select above to varchar:

SELECT (c.refullname || d.enname || f.evname)::varchar AS evlinkname1

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Gabor Siklos 2006-08-07 19:21:47 Does DROP TABLE free up disk space immediately?
Previous Message MargaretGillon 2006-08-07 19:11:36 Re: Varchar concatenate fields as Char or Varchar, not Text