Resp: RE: Resp: [SQL] Concatenation in selects

From: "Fidelis Assis" <fidelis(at)embratel(dot)com(dot)br> (Tel +55 21 519 8408)
To: owner-pgsql-sql(at)hub(dot)org, "Fidelis Assis" <fidelis(at)embratel(dot)com(dot)br>, owner-pgsql-sql(at)hub(dot)org, pgsql-sql(at)postgreSQL(dot)org
Subject: Resp: RE: Resp: [SQL] Concatenation in selects
Date: 1998-09-03 17:54:25
Message-ID: 4246531703091998/A01229/SCI02/11C91C752D00*@MHS
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

>
>> select ltrim(first_name) || (' ' || ltrim(last_name)) as full_name
>> from
>> ...
>>
>> where "ltrim" is used only to convert both, first_time and
>> last_time, to text. I wish I could convert among string types
>> (bpchar, varchar and text) without any tricks, but it seems this
>> is not implemented yet.
>>
>Ever tried the cast() function?
> -DEJ
>

Yes, but I haven´t been successful.

This is an example of what I have tried:

httpd=> create table t1 (f1 char(4), f2 varchar(4), f3 text);
CREATE
httpd=> \d t1

Table = t1
+----------------------------+----------------------------+-------+
| Field | Type | Length|
-----------------------------+----------------------------+-------+
| f1 | char() | 4 |
| f2 | varchar() | 4 |
| f3 | text | var |
+----------------------------+----------------------------+-------+

httpd=> insert into t1 values ('1234', '5678', '9012');
INSERT 648441 1

httpd=> select f1 || f1 from t1;
?column?
--------
12341234
(1 row)

OK, when both operands have the same type, but

httpd=> select f1::varchar || f2 from t1;
ERROR: Sep 3 16:53:00:function varchar(bpchar) does not exist
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

On the other hand, conversions to integer do work:

httpd=> select f1::int2 + f2::int2 from t1;
?column?
--------
6912
(1 row)

finally,

httpd=> select (f1 || f1) || f3 from t1;
ERROR: Sep 3 17:15:36:There is no operator '||' for types 'bpchar' and
'text'
You will either have to retype this query using an explicit cast,
or you will have to define the operator using CREATE OPERATOR

The result of (f1 || f1) is of type bpchar, but f3 is of type text.

httpd=> select (f2 || f2) || f3 from t1;
ERROR: Sep 3 17:16:20:There is no operator '||' for types 'varchar' and
'text'
You will either have to retype this query using an explicit cast,
or you will have to define the operator using CREATE OPERATOR

The result of (f2 || f2) is of type varchar, but f3 is of type text.

httpd=> select (f3 || f3) || f3 from t1;
?column?
------------
901290129012
(1 row)

OK, because all operands have the same type.

It´s interesting that, despite the documentation (\do) states that the
result of the operator "||" is of type text, it's allways resulting a
value with the same type as of its operands.

It´s worth noting that the function "textcat" works with any type of
operand.

Fidelis Assis
MCI-Embratel - Rio de Janeiro

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Jackson, DeJuan 1998-09-03 18:29:28 RE: [SQL] rename table - primary key index?
Previous Message Sergei Barbarash 1998-09-03 15:11:11 indexing datetime