X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 24FD3568BC for ; Tue, 15 Mar 2005 15:24:09 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 73990-02 for ; Tue, 15 Mar 2005 15:24:04 +0000 (GMT) Received: from smurfer.pastoraat (minas-2.demon.nl [212.238.194.105]) by svr1.postgresql.org (Postfix) with ESMTP id A723356920 for ; Tue, 15 Mar 2005 15:23:01 +0000 (GMT) Received: from [192.168.1.1] (athlon.pastoraat [192.168.1.1]) by smurfer.pastoraat (8.12.8/8.12.8) with ESMTP id j2FFalMM008189 for ; Tue, 15 Mar 2005 16:36:47 +0100 Message-ID: <4236FEF1.7050606@minas.demon.nl> Date: Tue, 15 Mar 2005 16:27:45 +0100 From: Michiel Lange User-Agent: Mozilla Thunderbird 0.8 (Windows/20040913) X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-admin@postgresql.org Subject: How to format a date with a serial number for DEFAULT? Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.05 tagged_above=0 required=5 tests=FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200503/223 X-Sequence-Number: 16721 Hello list, I am trying to create a table that hould countain a number formatted this way: YYYYMMDD########## Where the hashes should be padded to '0'. I have tried the following template_test=# CREATE TEMP TABLE test ( template_test(# counter SERIAL, template_test(# foobar CHAR(18) template_test(# DEFAULT CAST(date_part('year',current_date) AS TEXT) template_test(# || CAST(CAST(to_char(date_part('month',current_date),'00') AS INT) AS TEXT) template_test(# || CAST(CAST(to_char(date_part('day',current_date),'00') AS INT) AS TEXT) template_test(# || CAST(CAST(to_char(nextval('test_counter_seq'),'0000000000') AS INT) AS TEXT), template_test(# tekst TEXT); This resulted in something almost good, but I lost the padding zeroes. I got "20053151" Without the many CAST's like this: template_test=# CREATE TEMP TABLE test ( template_test(# counter SERIAL, template_test(# foobar CHAR(18) template_test(# DEFAULT CAST(date_part('year',current_date) AS TEXT) template_test(# || to_char(date_part('month',current_date),'00') template_test(# || to_char(date_part('day',current_date),'00') template_test(# || to_char(nextval('test_counter_seq'),'0000000000'), template_test(# tekst TEXT); Resulted in something almost right as well, but now to_char adds a space before each to_char I would get a result like "2005 03 05 0000000001" What options do I have to get this straight? Mind that I created TEMP tables to test how I should set my default value.... TIA Michiel