0

I want a sequence to start with AP0000001 and get incremented by 1 so the next value will be AP0000002 and so on

So following are the queries I tried for

  1. Creating sequence Create sequence patient_no_seq Start with 0000001 Increment by 1;

  2. Creating a table Create table patient( id serial not null primary key, patient_no text not null default('AP' || nextval('patient_no_seq'));

When I try inserting the patient_no is incrementing in AP1, AP2... in this manner but I want it to get incremented as AP0000001, AP0000002... and so on

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Random
  • 1
  • 1
  • Try to_char(nextval(...), '0000000') – mike.k Jun 15 '23 at 03:54
  • Instead of a sequence separate from the `id serial`, consider using a generated column. Having `id` and `patient_no` diverge is probably not what you want. – Bergi Jun 15 '23 at 04:05
  • Do you even *need* this sequence in the database? Generating the formatted patient number from the `id`, and conversely, parsing formatted numbers into integers, could (should?) be done in the application. – Bergi Jun 15 '23 at 04:07
  • Please [ask a new question](https://stackoverflow.com/questions/ask) instead of replacing your post with completely different content – Bergi Jul 24 '23 at 20:28

0 Answers0