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

Поиск
Список
Период
Сортировка
От Justin Graf
Тема Re: MSSQL to PostgreSQL - Issue trying to generate unique ID using actual date
Дата
Msg-id 4BA2758E.7080406@magwerks.com
обсуждение исходный текст
Ответ на MSSQL to PostgreSQL - Issue trying to generate unique ID using actual date  ("Ignacio Balcarce" <ignacio.balcarce@vivatia.com>)
Список 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.

В списке pgsql-sql по дате отправления: