Elm
Elm

Reputation: 1407

How do I add a calculated column in sql workbench / j

How do I add a calculated column in SQL workbench/j (as client for amazon redshift)

ALTER TABLE table_one
ADD COLUMN test_column
AS (
SELECT
(CASE WHEN LEFT(name,3) = "Ads" THEN "ok" ELSE "no" END) 
FROM table_one
)
VARCHAR(100) NULL;

I've also tried substituting the SELECT statement with a constant string value and it did not work.

Upvotes: 2

Views: 7292

Answers (2)

Michael Stern
Michael Stern

Reputation: 487

Adding for future seekers: MySQL 5.7 supports computed columns, which it refers to as "generated" columns. In MySQL Workbench you can add a formula by selecting the row in the Columns tab, clicking the 'Generated' button, and adding the formula (without an equals symbol at the start) in the Default/Expression field.

Upvotes: 2

Sachin
Sachin

Reputation: 2148

You can do this by :

    ALTER TABLE table_one
    ADD COLUMN test_column VARCHAR(100) NULL;

    GO;

then update all rows by :

UPDATE table_one
SET test_column = (CASE WHEN LEFT(name,3) = "Ads" THEN "ok" ELSE "no" END) 

Upvotes: 1

Related Questions