From: | Justin Graf <justin(at)magwerks(dot)com> |
---|---|
To: | Ignacio Balcarce <ignacio(dot)balcarce(at)vivatia(dot)com> |
Cc: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: MSSQL to PostgreSQL - Issue trying to generate unique ID using actual date |
Date: | 2010-03-18 18:55:18 |
Message-ID: | 4BA27716.1010806@magwerks.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
OOPS did not mean to click send
On 3/18/2010 12:53 PM, Ignacio Balcarce wrote:
>
> Justin,
>
> Thanks in advance for your email. I forgot to tell than everyday IDs
> must start from 0. So… sequence id would look like: YYYYMMDD 00000001,
> YYYYMMDD 00000002, etc.
>
> Is there any way to make this sequence start from 0 every day?
>
> Thanks & Regards,
>
> Ignacio
>
> ---------------------------------------------
>
>
>
--we need to create a table so we keep track sequence number and when to
reset the count
create table sequ_id ( id_number int, sequ_name char(25), date_lastrun
date);
--insert a record ;
insert into sequ_id values (1, 'thuban_seq', current_date);
--- Now for the function to generate the ID with the date leading
CREATE OR REPLACE FUNCTION THUBAN_SP_GENERATEID()
RETURNS VARCHAR
AS $$
declare creturn varchar ;
BEGIN
--now update the sequ_id table so we know the value we get makes sense,
Update sequ_id set id_number = 1 where sequ_name= 'thuban_seq' and
date_lastrun <> current_date;
--now we get the next build the ID go to the table get the current
value add some zeros in front and add the date to the front.
creturn = to_char( current_date, 'YYYYMMDD')::varchar || ' ' ||
(Select lpad( id_number::char, 7, '0' )::varchar from sequ_id where
sequ_name= 'thuban_seq' )
--update the sequence table
Update sequ_id set id_number = (id_number + 1) where sequ_name=
'thuban_seq';
--return the value
return creturn ;
END;
$$ LANGUAGE plpgsql;
this will do what you want.
now i have NOT tested this but should get you closer, inside of the
god awful code from before.
All legitimate Magwerks Corporation quotations are sent in a .PDF file attachment with a unique ID number generated by our proprietary quotation system. Quotations received via any other form of communication will not be honored.
CONFIDENTIALITY NOTICE: This e-mail, including attachments, may contain legally privileged, confidential or other information proprietary to Magwerks Corporation and is intended solely for the use of the individual to whom it addresses. If the reader of this e-mail is not the intended recipient or authorized agent, the reader is hereby notified that any unauthorized viewing, dissemination, distribution or copying of this e-mail is strictly prohibited. If you have received this e-mail in error, please notify the sender by replying to this message and destroy all occurrences of this e-mail immediately.
Thank you.
From | Date | Subject | |
---|---|---|---|
Next Message | Dmitriy Igrishin | 2010-03-18 21:43:57 | Emacs sql-postgres (please, sorry for question not about PostgreSQL). |
Previous Message | Justin Graf | 2010-03-18 18:48:46 | Re: MSSQL to PostgreSQL - Issue trying to generate unique ID using actual date |