Thread: Number formatting
Hi, Simple question, which is probably something obvious I'm missing. If anyone can tell me better how to do what I'm doing, go for it! This is for a billing system, and I want PostgreSQL to generate unique reference numbers for each invoice that is something more meaningful than 1, 2, 3 etc. So, what I'm trying to do is use a primary key to generate invoice numbers such as "INV0000001", "INV0000002" etc. Obviously a sequence generates the increments, but when I try formatting it, it adds a space at the beginning of the number. Here's the "default value" for the primary key field: (('INV'::text || to_char((nextval('customer_customer_id_seq'::regclass))::integer, '0000000'::text)))::character varying However what I end up with is: "INV 0000012" If I take the "INV" portion out, and simply try and format the sequence generated, I still get the space added: " 0000013" (to_char((nextval('customer_customer_id_seq'::regclass))::integer, '0000000'::text))::character varying The field is defined as a varchar(10), which allows the 3-digit "INV", and 7 numeric digits. I have tried defining it as char(10) but still get the same. This is on PostgreSQL 8.1.4, Windows XP. Thanks Andy
On Sat, Jun 10, 2006 at 07:15:02PM +0100, Andy Shellam wrote: > So, what I'm trying to do is use a primary key to generate invoice > numbers such as "INV0000001", "INV0000002" etc. Obviously a sequence > generates the increments, but when I try formatting it, it adds a space > at the beginning of the number. The FM modifier suppresses padding spaces. http://www.postgresql.org/docs/8.1/interactive/functions-formatting.html#FUNCTIONS-FORMATTING-DATETIMEMOD-TABLE test=> SELECT 'INV' || to_char(123, 'FM0000000'); ?column? ------------ INV0000123 (1 row) -- Michael Fuhr
Perfect Thanks Michael :-) Andy Michael Fuhr wrote: > On Sat, Jun 10, 2006 at 07:15:02PM +0100, Andy Shellam wrote: >> So, what I'm trying to do is use a primary key to generate invoice >> numbers such as "INV0000001", "INV0000002" etc. Obviously a sequence >> generates the increments, but when I try formatting it, it adds a space >> at the beginning of the number. > > The FM modifier suppresses padding spaces. > > http://www.postgresql.org/docs/8.1/interactive/functions-formatting.html#FUNCTIONS-FORMATTING-DATETIMEMOD-TABLE > > test=> SELECT 'INV' || to_char(123, 'FM0000000'); > ?column? > ------------ > INV0000123 > (1 row) >