How to format a date with a serial number for DEFAULT?

Поиск
Список
Период
Сортировка
От Michiel Lange
Тема How to format a date with a serial number for DEFAULT?
Дата
Msg-id 4236FEF1.7050606@minas.demon.nl
обсуждение исходный текст
Список pgsql-admin
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

В списке pgsql-admin по дате отправления:

Предыдущее
От: Fred Blaise
Дата:
Сообщение: grant with pl/pgsql script
Следующее
От: Brad Nicholson
Дата:
Сообщение: Re: Performance Question