0

I got a table in which a column named items and its value is '' which is around 1000 of rows, how can we query in PostgreSQL using stored procedure to rename each items to a unique value like column with an incremental value i.e

 items0001, items0002, items003,.....items1000

Tried this but its not working

UPDATE product SET items=CASE
          WHEN items='' THEN  items='items'+1
          ELSE items123

          END
1
  • Have you tried something? If yes then, what have you tried? Commented Oct 9, 2012 at 10:19

3 Answers 3

1

Assuming your table has a primary key column named id the following should do it:

update product
    set items = new_items.new_item
from (
  select id, coalesce(items,'')||to_char(row_number() over (), 'FM00009') as new_item
  from product
) as new_items
where new_items.id = p.id
and product.items is null or product.items = '';
Sign up to request clarification or add additional context in comments.

2 Comments

@dude: you didn't mention anything regarding null in your question. see my edit
@dude: apparently you are using an outdated version of Postgres (probably 8.3 or even older).
0
UPDATE product SET items=CASE
      WHEN len(items)>0 THEN  items='items'+1
      ELSE items123

      END

2 Comments

The OP wants to update that column with an incremental value i.e items0001, items0002, items003, ...
but i got items like tanker,puzzles etc in the column name... i just want to update the row which has a value null
0

UPDATE product SET items=CASE WHEN len(items)>0 THEN 'items' || somesequence.nextval ELSE items END

Comments

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.