Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id F3D006328C8 for ; Wed, 13 Jan 2010 09:57:35 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 82117-10 for ; Wed, 13 Jan 2010 13:57:04 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from ey-out-2122.google.com (ey-out-2122.google.com [74.125.78.25]) by mail.postgresql.org (Postfix) with ESMTP id 005A5633865 for ; Wed, 13 Jan 2010 09:55:42 -0400 (AST) Received: by ey-out-2122.google.com with SMTP id 9so129450eyd.3 for ; Wed, 13 Jan 2010 05:55:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=u9TC1gL3ZsvJRxKDpWVWzABWqfJKg9M8uROYLsyuE3A=; b=lsvlqNUFiWdtdvIPyPjFIzVbuFYwOYt+mRC0T1GG1gVgPTlpCY6M9uWlwHvIKoPWbM mOVZapgSQf1ZCGVnjFXcVmod9Ov2Adcw2JuAcTd1Msr2Vu/XII2iFTJrN7TjSnPWj6Pw UZkBJOHiuI8Pqkc4KdsOhaftvtipgpQf3hmX4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=H/CBCfDFT4/xRWwKFXcB7+ZAWhTADAZP9HQLsk00PkfOg2pLBiNcigwDFakjfpbX7O h2ULx2WCbKKudod18XST0/fKkMXJjk7tboYS7RrV7ScuPnJ9HxunMV3e4+umkC6pSOZJ dFD291eLyqtI2EfmZwom0mRuL7p3YUjOWAE1o= MIME-Version: 1.0 Received: by 10.216.89.129 with SMTP id c1mr2309796wef.35.1263390940551; Wed, 13 Jan 2010 05:55:40 -0800 (PST) In-Reply-To: <162867791001130536w4a14520bobc67d1c7e7e47862@mail.gmail.com> References: <65d71e1c1001130102j5f46299cm87477efcfd0c5a51@mail.gmail.com> <162867791001130536w4a14520bobc67d1c7e7e47862@mail.gmail.com> Date: Wed, 13 Jan 2010 14:55:40 +0100 Message-ID: <162867791001130555s42c369dif0b5ccd668f08923@mail.gmail.com> Subject: Re: Substring auto trim From: Pavel Stehule To: "Charles O'Farrell" Cc: pgsql-bugs@postgresql.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.095 tagged_above=-10 required=5 tests=AWL=0.504, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201001/110 X-Sequence-Number: 25614 2010/1/13 Pavel Stehule : > Hello > > 2010/1/13 Charles O'Farrell : >> Hi guys, >> >> I'm not sure whether this a really dumb question, but I'm curious as to = what >> might be the problem. >> >> We have a column 'foo' which is of type character (not varying). >> >> select substr(foo, 1, 10) from bar >> >> The result of this query are values whose trailing spaces have been trim= med >> automatically. This causes incorrect results when comparing to a value t= hat >> may contain trailing spaces. >> >> select * from bar where substr(foo, 1, 4) =3D 'AB=C2=A0 ' >> > > You have to write C function substr for type "any" :( Because "char" > and char(n) are two different types, and you cannot to write function > for char(n) > > >> I should mention that we normally run Oracle and DB2 (and have done for = many >> years), but I have been pushing for Postgres as an alternative. >> Fortunately this is all handled through Hibernate, and so for now I have >> wrapped the substr command in rpad which seems to do the trick. >> >> Any light you can shed on this issue would be much appreciated. >> I thing, so there is workaround, create or replace function substr(character, int, int) returns character as= $$ select substr($1::cstring::text,$2,$3) $$ language sql; postgres=3D# create table f(a character(5)); CREATE TABLE postgres=3D# insert into f values('a'),('ab'),('abc'); INSERT 0 3 postgres=3D# select * from f; a ------- a ab abc (3 rows) postgres=3D# select * from f where substr(a,1,3) =3D 'a '; a ------- a (1 row) postgres=3D# select * from f where substr(a,1,3) =3D 'ab '; a ------- ab (1 row) Regards Pavel Stehule > > Function substr has first parameter of type "text". When pg call this > function, then it does conversion from char(x) to text. > > Regards > Pavel Stehule > > >> Cheers, >> >> Charles O'Farrell >> >> PostgreSQL 8.4.2 on i486-pc-linux-gnu, compiled by GCC gcc-4.4.real (Ubu= ntu >> 4.4.1-4ubuntu8) 4.4.1, 32-bit >> >