Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id C7F63632277 for ; Thu, 18 Mar 2010 14:02:21 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 81656-06 for ; Thu, 18 Mar 2010 17:02:09 +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 4C2B66321FD for ; Thu, 18 Mar 2010 14:02:10 -0300 (ADT) Received: from [192.168.1.36] by mail.magwerks.com (GMS 15.01.3664/RS2750.00.1b502c7c) with ESMTP id cduceaaa for pgsql-sql@postgresql.org; Thu, 18 Mar 2010 13:02:06 -0400 Message-ID: <4BA25C8C.10305@magwerks.com> Date: Thu, 18 Mar 2010 12:02:04 -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> In-Reply-To: <000001cac5e1$8851dbe0$98f593a0$@balcarce@vivatia.com> Content-Type: multipart/alternative; boundary="------------030104010406000403070309" 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.097 tagged_above=-10 required=5 tests=AWL=0.501, BAYES_00=-2.599, HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 201003/149 X-Sequence-Number: 33856 This is a multi-part message in MIME format. --------------030104010406000403070309 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit On 3/17/2010 9:52 AM, Ignacio Balcarce wrote: > > Hi all, > > I am facing a problem trying to convert from MSSQL procedure to > PostgreSQL function. > > CREATE PROCEDURE dbo.THUBAN_SP_GENERATEID > > @NEWID VARCHAR(20) OUTPUT > > AS > > SET @NEWID = ( > > SELECT REPLACE(SUBSTRING(CONVERT(CHAR(10),GETDATE(),20 ),1,10),'-','') > > + CAST(REPLICATE(0,8-LEN (ISNULL(CAST(SUBSTRING(MAX(SEQ_ID),9,8) AS > > INTEGER),0) + 1)) AS VARCHAR) > > + CAST(ISNULL(CAST(SUBSTRING(MAX(SEQ_ID),9,8) AS INTEGER),0) + 1 AS > > VARCHAR) > > FROM THUBAN_SEQ > > WHERE SUBSTRING(SEQ_ID,1,8)= > > REPLACE(SUBSTRING(CONVERT(CHAR(10),GETDATE(),20 ),1,10),'-','') > > ) > > INSERT INTO THUBAN_SEQ VALUES (@NEWID) > > SELECT @NEWID AS ITEM_ID; > > GO > I surprised this works in MSSQL CREATE SEQUENCE THUBAN_SEQ INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 START 1 CACHE 1; Now for the function to generate the ID with the date leading CREATE OR REPLACE FUNCTION THUBAN_SP_GENERATEID() RETURNS VARCHAR AS $$ BEGIN --now we get the next value from the thuban_seq and add the date to the front. return to_char( current_timestamp, 'MMDDYYYY')::varchar || nextval('THUBAN_SEQ')::varchar RETURN NEWID; END; $$ LANGUAGE plpgsql; If this is not what your after you need to give more information what you want to accomplish 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. --------------030104010406000403070309 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: 7bit On 3/17/2010 9:52 AM, Ignacio Balcarce wrote:

Hi all,

 

I am facing a problem trying to convert from MSSQL procedure to PostgreSQL function.

 

CREATE PROCEDURE dbo.THUBAN_SP_GENERATEID

 

         @NEWID VARCHAR(20)  OUTPUT

AS

 

 

 

 

        SET @NEWID = (

                        SELECT REPLACE(SUBSTRING(CONVERT(CHAR(10),GETDATE(),20 ),1,10),'-','')

                                + CAST(REPLICATE(0,8-LEN (ISNULL(CAST(SUBSTRING(MAX(SEQ_ID),9,8) AS

INTEGER),0) + 1)) AS VARCHAR)

                                        + CAST(ISNULL(CAST(SUBSTRING(MAX(SEQ_ID),9,8) AS INTEGER),0) + 1  AS

VARCHAR)

                        FROM THUBAN_SEQ

                        WHERE SUBSTRING(SEQ_ID,1,8)=

REPLACE(SUBSTRING(CONVERT(CHAR(10),GETDATE(),20 ),1,10),'-','')

                        )

 

 

        INSERT INTO THUBAN_SEQ VALUES (@NEWID)

 

        SELECT @NEWID AS ITEM_ID;

GO

 



I surprised this works in MSSQL

CREATE SEQUENCE THUBAN_SEQ
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;


Now for the function to generate the ID with the date leading

CREATE OR REPLACE FUNCTION THUBAN_SP_GENERATEID()
RETURNS VARCHAR

AS $$

BEGIN

--now we get the next value from the thuban_seq and add the date to the  front. 

return  to_char( current_timestamp,   'MMDDYYYY')::varchar || nextval('THUBAN_SEQ')::varchar

               

RETURN NEWID;

END;

$$ LANGUAGE plpgsql;

If this is not what your after you need to give more information what you want to accomplish




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.
--------------030104010406000403070309--