Re: MSSQL to PostgreSQL - Issue trying to generate unique ID using actual date

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:48:46
Message-ID: 4BA2758E.7080406@magwerks.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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 );

--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 $$

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 value from the thuban_seq and add the date to
the front.

return to_char( current_date, 'YYYYMMDD')::varchar || ' ' ||
(Select lpad( id_number::char, 7, '0' )::varchar from sequ_id where
sequ_name= 'thuban_seq' and date_lastrun)

Update sequ_id set id_number = (id_number + 1) where sequ_name=
'thuban_seq';

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.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Justin Graf 2010-03-18 18:55:18 Re: MSSQL to PostgreSQL - Issue trying to generate unique ID using actual date
Previous Message Ignacio Balcarce 2010-03-18 17:53:52 Re: MSSQL to PostgreSQL - Issue trying to generate unique ID using actual date