Elm Elm - 1 month ago 7
SQL Question

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.

Thanks

Answer

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) 
Comments