Case statement with different data types

From: Gus Waddell <angus(dot)waddell(at)palcare(dot)com(dot)au>
To: pgsql-general(at)postgresql(dot)org
Subject: Case statement with different data types
Date: 2009-10-14 07:59:00
Message-ID: 4AD584C4.7020208@palcare.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body bgcolor="#ffffff" text="#000000">
Hi everyone,<br>
<br>
I am currently working on an app that is split into several databases
with the same table but a column with differing data type.<br>
<br>
eg. Database 1 <br>
tablename: gp<br>
column:&nbsp; available - data type boolean<br>
<br>
Database 2<br>
tablename: gp<br>
column:&nbsp; available - data type character(1)<br>
<br>
I would like to be able to create a query that returns 'T' or 'F' using
the same code &amp; query.<br>
<br>
I have tried a few different cast() options without success - and then
tried:<br>
<br>
<font color="#000066"><small>SELECT<br>
&nbsp;&nbsp;&nbsp; CASE<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN ((SELECT data_type FROM information_schema.columns WHERE
table_name = 'gp' and column_name = 'available') = 'boolean') THEN <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; CASE<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN (available) THEN 'T'<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ELSE 'F'<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; END <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ELSE <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; CASE<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN (available='T' OR available='t') then 'T'<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ELSE 'F'<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; END<br>
&nbsp;&nbsp;&nbsp; END as available<br>
FROM<br>
&nbsp;&nbsp;&nbsp; gp<br>
</small></font><br>
<br>
When I run this query on the boolean data type database it works
correctly - however on the character field I get the error <font
color="#ff0000"><small>'ERROR:&nbsp; argument of CASE/WHEN must be type
boolean, not type character'</small></font><br>
<br>
I really don't want to go through and change data types just at the
moment - can anyone advise any way around this?<br>
<br>
Many thanks,<br>
Gus<br>
<br>
</body>
</html>

Attachment Content-Type Size
unknown_filename text/html 2.1 KB

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Scott Marlowe 2009-10-14 08:02:13 Re: Cannot start the postgres service
Previous Message Paul Hartley 2009-10-14 07:12:44 different sort order for primary key index