Re: CHAR LIKE VARCHAR

From: Richard Huxton <dev(at)archonet(dot)com>
To: ilejn(at)yandex(dot)ru
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: CHAR LIKE VARCHAR
Date: 2005-06-07 09:03:21
Message-ID: 42A562D9.7040001@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Ilja Golshtein wrote:
> Hi!
>
> I came across following difference
> between "LIKE" and "=" regarding
> CHARs and VARCHARs
>
> create table aa(f5 char(5), fv varchar(5));
> insert into aa values('str1', 'str1');
> select count(*) from aa where f5 = fv;
>
>>1
>
> select count(*) from aa where f5 like fv;
>
>>0
>
> I understand trailing spaces in CHAR are not
> significant though I expect DBMS shows
> consistent (and, ideally, clearly documented)
> behavior.

If you ask me (and it's too late to back out now :-) the whole behaviour
of CHAR(n) is wrong, broken and just a bad idea.

>>From my point of view in example above
> it would be nice to have the same result
> for both queries regardless it is 0 or 1.
>
> Of course, I may be wrong. Is there a clear
> concept behind the difference between "LIKE"
> and "="?

Well, you could argue that LIKE should ignore the trailing spaces (and
the only way to decide is to look at the SQL specs). The problem is,
that if f5=fv then presumably length(f5)=length(fv) and length(f5 ||
'+') = length(f5)+1

Of course, only the first of these is true because the whole idea of
char(n) is badly thought out. Either the value contains spaces or it
doesn't - unfortunately it's neither and both.
--
Richard Huxton
Archonet Ltd

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Michael Glaesemann 2005-06-07 09:24:44 Re: CHAR LIKE VARCHAR
Previous Message Jochem van Dieten 2005-06-07 08:56:51 Re: pg_dump 8.0.3 failing against PostgreSQL 7.3.2