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

 

De: Justin Graf [mailto:jus...@magwerks.com] 
Enviado el: Jueves, 18 de Marzo de 2010 02:02 p.m.
Para: Ignacio Balcarce
CC: pgsql-sql@postgresql.org
Asunto: Re: [SQL] MSSQL to PostgreSQL - Issue trying to generate unique ID 
using actual date

 

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. 

Reply via email to