1

I have a table with several columns. I want to create a new column from an existing one. Say column "A" is text, I want to insert into column "B" the left 10 characters of column "A".

I do:

insert into table (B) select left(A, 10) from table;

However, I want this to be aligned with column "A" not as new rows, how do I do that?

1 Answer 1

2

I want this to be aligned with column "A" not as new rows

Insert adds new rows. Since you need to update existing ones, use UPDATE:

UPDATE MyTable
SET B = LEFT(A, 10)
Sign up to request clarification or add additional context in comments.

3 Comments

That was so simple. Thank you!
ERROR: cannot use column references in default expression + I don't think that PostgreSQL supports computed/generated columns yet.
@lad2025 Right, Postgres doesn't allow it. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.