Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id A892B6328CD for ; Thu, 18 Mar 2010 15:49:05 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 62834-08 for ; Thu, 18 Mar 2010 18:48:54 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.magwerks.com (mail.magwerks.com [209.248.233.131]) by mail.postgresql.org (Postfix) with ESMTP id 3AB8E632330 for ; Thu, 18 Mar 2010 15:48:54 -0300 (ADT) Received: from [192.168.1.36] by mail.magwerks.com (GMS 15.01.3664/RS2750.00.1b502c7c) with ESMTP id xquceaaa for pgsql-sql@postgresql.org; Thu, 18 Mar 2010 14:48:49 -0400 Message-ID: <4BA2758E.7080406@magwerks.com> Date: Thu, 18 Mar 2010 13:48:46 -0500 From: Justin Graf User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.8) Gecko/20100227 Lightning/1.0b1 Thunderbird/3.0.3 MIME-Version: 1.0 To: Ignacio Balcarce CC: pgsql-sql@postgresql.org Subject: Re: MSSQL to PostgreSQL - Issue trying to generate unique ID using actual date References: <000001cac5e1$8851dbe0$98f593a0$@balcarce@vivatia.com> <4BA25C8C.10305@magwerks.com> <000b01cac6c3$f9f58520$ede08f60$@balcarce@vivatia.com> In-Reply-To: <000b01cac6c3$f9f58520$ede08f60$@balcarce@vivatia.com> Content-Type: multipart/alternative; boundary="------------050706010405010600040504" X-AntiSpam: Checked for restricted content by Gordano's AntiSpam Software X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.598 tagged_above=-10 required=5 tests=BAYES_00=-2.599, HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 201003/150 X-Sequence-Number: 33857 This is a multi-part message in MIME format. --------------050706010405010600040504 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit 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. --------------050706010405010600040504 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: 8bit 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.
--------------050706010405010600040504--