X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 803DE52AAD for ; Fri, 11 Mar 2005 22:17:01 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 89161-09 for ; Fri, 11 Mar 2005 22:17:00 +0000 (GMT) Received: from mail.gmx.net (pop.gmx.de [213.165.64.20]) by svr1.postgresql.org (Postfix) with SMTP id 7A5CD563B3 for ; Fri, 11 Mar 2005 22:16:59 +0000 (GMT) Received: (qmail invoked by alias); 11 Mar 2005 22:17:00 -0000 Received: from p5489F831.dip.t-dialin.net (EHLO gontscharow.geoval.de) (84.137.248.49) by mail.gmx.net (mp029) with SMTP; 11 Mar 2005 23:17:00 +0100 X-Authenticated: #11051374 From: Marco Manfredini To: pgsql-sql@postgresql.org Subject: Re: Conver bool to text Date: Fri, 11 Mar 2005 23:12:28 +0100 User-Agent: KMail/1.6.2 MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Message-Id: <200503112312.28266.mldb@gmx.net> X-Y-GMX-Trusted: 0 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.05 tagged_above=0 required=5 tests=FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200503/165 X-Sequence-Number: 20644 Edmund Bacon wrote: > Is there a way to convert a boolean value to text and/or vice versa? > [cause it doesn't work as expected] > > It's not that difficult to write a fuction to convert boolean to text, > but I'm wondering if there's already something that does this? You can help yourself by misusing the $type_in and $type_out functions: create or replace function bool2text(bool) returns text as $$ select textin(boolout($1)); $$ language sql; create cast (bool as text) with function bool2text(bool); This is somewhat generic. Since the name of the *in and *out function can be read from pg_type, the conversion function can even be generated automatic: /* warning: conversion via text representation isn't always right. */ create or replace function make_conversion_function(s text, d text) returns void as $$ declare tin text; tout text; xp text; begin select typinput into tin from pg_catalog.pg_type where typname=d; select typoutput into tout from pg_catalog.pg_type where typname=s; xp:='create or replace function as_' || d || '(' || s || ') returns ' || d || ' as $BODY$ select ' || tin || '(' || tout || '($1)) $BODY$ language sql;'; execute xp; /* create cast analogue..*/ return; end; $$ language plpgsql; select make_conversion_function('text','bool'); select make_conversion_function('bool','text'); select as_bool('true'),as_text(true); --- Marco