pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
function date_part
2+ messages / 2 participants
[nested] [flat]

* function date_part
@ 2000-04-11 13:07  Andreas Stahlhut <PDH.KFI3@t-online.de>
  0 siblings, 1 reply; 2+ messages in thread

From: PDH.KFI3@t-online.de @ 2000-04-11 13:07 UTC (permalink / raw)
  To: pgsql-sql

Hi,

I need a primary key (int4) in a table which is build from the actual year and a
serial number like 20000001 (year 2000 and the serial number 0001).
If I get the function date_part to build this number like
    select 
       int4(date_part('year', 'now'::datetime) * 10000 + nextval('serial_seq'))
    as primkey; 
it works.
If I use this in a create table like
    create table primtab(
       primkey
            int4 
	primary key
            default 
            int4(date_part('year', 'now'::datetime) * 1000+nextval('serial_seq'))
    ); 
it never works.
	ERROR:  parser: parse error at or near "'"

If I use it in a create function like
    create function primfnc() returns int4
      as 'select .... as primnum'
      language 'sql';
it never works.
	ERROR:  parser: parse error at or near "year"

Is there a mistake in the SQL-QUERY's or isn't it implemented?

Thanks for an answer

Andreas
 -- 
Andreas Stahlhut                                stahlhut@talkline.de
Diplomverwaltungswirt
PD Hannover, ZKD, 3.2 K                         PDH.KFI3@t-online.de
Alter Flughafen 18a                             Tel (0511) 109-5377
30179 Hannover                                  Fax (0511) 109-5330



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: function date_part
@ 2000-04-11 15:29  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Andreas Stahlhut <PDH.KFI3@t-online.de>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2000-04-11 15:29 UTC (permalink / raw)
  To: PDH.KFI3@t-online.de; +Cc: pgsql-sql

PDH.KFI3@t-online.de (Andreas Stahlhut) writes:
> If I use this in a create table like
>     create table primtab(
>        primkey
>             int4 
> 	primary key
>             default 
>             int4(date_part('year', 'now'::datetime) * 1000+nextval('serial_seq'))
>     ); 
> it never works.
> 	ERROR:  parser: parse error at or near "'"

The default-value support in 6.5.* is pretty limited, not to say broken;
I think it is probably failing on the '::datetime' part of that
expression.  7.0 copes just fine.  As a workaround for 6.5, you might
try writing date_part('year', now()) instead.

> If I use it in a create function like
>     create function primfnc() returns int4
>       as 'select .... as primnum'
>       language 'sql';
> it never works.
> 	ERROR:  parser: parse error at or near "year"

This is probably pilot error.  Did you remember to double the ' symbols
in the body of the function definition?  Remember the body is itself
a single-quoted string, so any embedded quote marks must be written ''
(or \' if you prefer).

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2000-04-11 15:29 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2000-04-11 13:07 function date_part Andreas Stahlhut <PDH.KFI3@t-online.de>
2000-04-11 15:29 ` Tom Lane <tgl@sss.pgh.pa.us>

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