Re: Case insensitive hstore.

From: Ian Lawrence Barwick <barwick(at)gmail(dot)com>
To: Glenn Pierce <glennpierce(at)gmail(dot)com>
Cc: PG-General Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Re: Case insensitive hstore.
Date: 2013-02-16 00:12:37
Message-ID: CAB8KJ=iu02DupMkLe-gmQb+asvoQvBayma4x3ff4nLkf8W-tNg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

2013/2/16 Glenn Pierce <glennpierce(at)gmail(dot)com>:
> Hi
>
> Does anyone know how one would
> select from a table with a hstore field treating the key of the hstore as
> case insensitive.
>
> ie
>
> SELECT id, lower(additional_info->'type') AS type FROM table
>
> I would like this to work even if if the store tyoe is
>
> 'Type' -> 'original'

As far as I can see from looking at the docs, it's not possible (I
could be wrong though).

> failing that is there a way to lowercase the keys and values of the hstore
> field of the entire table ?

You could create a function like this:

CREATE OR REPLACE FUNCTION hstore_to_lower(val HSTORE)
RETURNS HSTORE
LANGUAGE plpgsql
AS $function$
DECLARE
hkey TEXT;
BEGIN
FOR hkey IN
SELECT SKEYS(val)
LOOP

IF LOWER(hkey) != hkey THEN
val := val || (LOWER(hkey) || '=>' ||
LOWER((val->hkey::TEXT)))::HSTORE;
val := val - hkey;
END IF;

END LOOP;
RETURN val;
END;
$function$

No guarantee of suitability for a particular purpose or of it being
the optimal way of
doing this ;)
Note that any keys converted to lower case will overwrite existing
lower case keys.

HTH

Ian Barwick

In response to

Browse pgsql-general by date

  From Date Subject
Next Message René Romero Benavides 2013-02-16 01:01:20 Re: PG9.2.3. Query hanging: SELECT count(*) FROM pg_catalog.pg_class...
Previous Message David Kerr 2013-02-15 21:58:55 PG9.2.3. Query hanging: SELECT count(*) FROM pg_catalog.pg_class...