Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql 9.4 Alter Column Text to Boolean with Values

Tags:

postgresql

I have a table that looks like this:

CREATE TABLE "TestResults" (
"Id"                text PRIMARY KEY,
"Name"              text NOT NULL UNIQUE,
"Result"            text CHECK ("Comment" IN ('Pass', 'Fail')),
"CreatedBy"         text NOT NULL,
"CreatedOn"         timestamp with time zone NOT NULL,
);

We are using PostgreSQL 9.4

The user is currently able to select either Pass or Fail from a drop down menu, and we have been storing those strings in the database in the Result column. We would like to change that to a boolean value.

How can I change the Result column from text to boolean while keeping the values the users have already entered?

Thank you.

like image 471
mkhira2 Avatar asked Feb 20 '18 02:02

mkhira2


People also ask

How do you write a boolean in PostgreSQL?

Boolean constants can be represented in SQL queries by the SQL key words TRUE , FALSE , and NULL . Unique prefixes of these strings are also accepted, for example t or n . Leading or trailing whitespace is ignored, and case does not matter.

How do I change the datatype of a column in PostgreSQL?

First, specify the name of the table to which the column you want to change belongs in the ALTER TABLE clause. Second, give the name of column whose data type will be changed in the ALTER COLUMN clause. Third, provide the new data type for the column after the TYPE keyword.

What is the difference between Bool and boolean in PostgreSQL?

PostgreSQL Boolean is a simple data type that we have used to represent only the structure of true or false data or values. PostgreSQL will support the SQL99 defined Boolean data type of SQL standard; Boolean is also known as “bool”, bool is an alias of Boolean data type in PostgreSQL.

Which one is correct altering a column in PostgreSQL?

The syntax to modify a column in a table in PostgreSQL (using the ALTER TABLE statement) is: ALTER TABLE table_name ALTER COLUMN column_name TYPE column_definition; table_name. The name of the table to modify.


1 Answers

Any time you want to change a column's type but there is no default conversion from the old values to the new ones, you want to use a USING clause to specify how to convert the old values to the new ones:

The optional USING clause specifies how to compute the new column value from the old; if omitted, the default conversion is the same as an assignment cast from old data type to new. A USING clause must be provided if there is no implicit or assignment cast from old to new type.

So first get rid of the CHECK constraint, you can find the name by doing a \d "TestResults" from psql but it is probably "TestResults_Result_check":

alter table "TestResults" drop constraint "TestResults_Result_check";

Then an ALTER COLUMN (with a USING clause) to change the type:

alter table "TestResults"
alter column "Result"
set data type boolean
using case
    when "Result" = 'Pass' then true
    when "Result" = 'Fail' then false
    else null
end;
like image 106
mu is too short Avatar answered Nov 15 '22 09:11

mu is too short