Re: Convert boolean field to a 0 or a 1 in a select statement

From: Steve Crawford <scrawford(at)pinpointresearch(dot)com>
To: JORGE MALDONADO <jorgemal1960(at)gmail(dot)com>, pgsql-novice(at)postgresql(dot)org
Subject: Re: Convert boolean field to a 0 or a 1 in a select statement
Date: 2015-01-09 16:54:27
Message-ID: 54B007C3.7000600@pinpointresearch.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On 01/09/2015 08:47 AM, JORGE MALDONADO wrote:
> I have a SELECT statement which contains a boolean field. The result
> of this query must display '0' for false or '1' for true. I will be
> generating a text file with this information and a zero or a one is
> required. I have tried using the CASE statement without success.
>
> CAST(fce_subdivision as CHAR) AS fce_subdivision_aux,
> CASE CAST(fce_subdivision as CHAR)
> WHEN 'f' THEN '0'
> WHEN 't' THEN '1'
> END
>
>

I recommend reading the docs to ensure this casting is explicitly
documented so it won't stop working in future releases but this works
for me:
select true::int:char(1);

Where you would replace "true" with the name of your boolean field.

Cheers,
Steve

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Steve Crawford 2015-01-09 17:15:37 Re: Upgrade Ubuntu - PostgresSQL is ok?
Previous Message JORGE MALDONADO 2015-01-09 16:47:11 Convert boolean field to a 0 or a 1 in a select statement