pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrei Bintintan <klodoma@ar-sd.net>
To: Michiel Lange <michiel@minas.demon.nl>
To: pgsql-admin@postgresql.org
Subject: Re: How to format a date with a serial number for DEFAULT?
Date: Tue, 15 Mar 2005 18:14:51 +0200
Message-ID: <011401c5297a$22fcf800$0b00a8c0@forge> (raw)
References: <4236FEF1.7050606@minas.demon.nl>

CREATE TABLE test(
counter SERIAL,
foobar CHAR(100)
DEFAULT to_char(CURRENT_DATE, 'DDMMYYYY') || 
trim(to_char(nextval('test_counter_seq'),'0000000000')),
tekst TEXT);

I don't know exactly why the white space is in, but the trim function takes 
it out.

Best regards,
Andy.

----- Original Message ----- 
From: "Michiel Lange" <michiel@minas.demon.nl>
To: <pgsql-admin@postgresql.org>
Sent: Tuesday, March 15, 2005 5:27 PM
Subject: [ADMIN] How to format a date with a serial number for DEFAULT?


> 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
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
>
> 





view thread (5+ messages)  latest in thread

Message-ID: <011401c5297a$22fcf800$0b00a8c0@forge>
Permalink:  ../011401c5297a$22fcf800$0b00a8c0@forge/
Also on:    postgresql.org/message-id/011401c5297a$22fcf800$0b00a8c0@forge

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: klodoma@ar-sd.net, michiel@minas.demon.nl
  Subject: Re: How to format a date with a serial number for DEFAULT?
  In-Reply-To: <011401c5297a$22fcf800$0b00a8c0@forge>
  References: <4236FEF1.7050606@minas.demon.nl> <011401c5297a$22fcf800$0b00a8c0@forge>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox