Обсуждение: MSSQL to PostgreSQL - Issue trying to generate unique ID using actual date
MSSQL to PostgreSQL - Issue trying to generate unique ID using actual date
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
This is what I made,
CREATE OR REPLACE FUNCTION THUBAN_SP_GENERATEID()
RETURNS VARCHAR
AS $$
DECLARE NEWID VARCHAR;
DECLARE SEQID VARCHAR;
BEGIN
SELECT INTO NEWID TO_CHAR(CURRENT_DATE::DATE, 'YYYYMMDD');
-- IF EXISTS A ROW IN THE TABLE STARTING WITH THE CURRENT_DATE, SELECT THE MAX OF THEM.
IF EXISTS(SELECT(MAX(SEQ_ID)) FROM THUBAN_SEQ WHERE SEQ_ID LIKE (SELECT TO_CHAR(CURRENT_DATE::DATE, 'YYYYMMDD') || '%')) THEN
SELECT INTO NEWID ((SELECT(MAX(SEQID)) FROM THUBAN_SEQ WHERE SEQ_ID LIKE NEWID || '%') + 1);
ELSE
-- THIS IS NOT RIGHT AT ALL, RIGHT? HOW CAN I DO TO CONCATENATE AN INTEGER NUMBER LIKE 14 + SOME NUMBER OF 0 BEFORE?
SEQID := '00000001';
NEWID := NEWID + SEQID;
END IF;
RETURN NEWID;
END;
$$ LANGUAGE plpgsql;
SELECT THUBAN_SP_GENERATEID();
Beside this, there is something than I would like to ask than I couldn’t find. How can I do to set a variable in a way like this as MSSQL does:
SET @NEWID = (SELECT……
And not doing SELECT INTO VARIABLE_TO_SET (SELECT…..
All comments will be welcome, I am pretty new with PostgreSQL but I find It very interesting.
Thanks & Regards,
Ignacio
On 03/17/10 17:52, Ignacio Balcarce wrote: > 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 > At a first glance it looks like an INSERT INTO thuban_seq(seq_id) VALUES (your_strange_string_processing( now() )) RETURNING seq_id; But i couldn't interpret your extremely strange string processing with dates. Please, FIRST OF ALL, get rid of this unnecessary brainfuck, use postgres date-time arithmetic and clarify the idea of this routine.
On 03/17/10 17:52, Ignacio Balcarce wrote: > -- IF EXISTS A ROW IN THE TABLE STARTING WITH THE CURRENT_DATE Sorry, your field is not an atom => your database does not met a FIRST normal form. it needs normalization urgently.
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.
--we need to create a table so we keep track sequence number and when to reset the countJustin,
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
---------------------------------------------
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.
On 3/18/2010 12:53 PM, Ignacio Balcarce wrote:
--we need to create a table so we keep track sequence number and when to reset the countJustin,
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
---------------------------------------------
create table sequ_id ( id_number int, sequ_name char(25), date_lastrun date);
--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 $$
declare creturn varchar ;
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 build the ID go to the table get the current value add some zeros in front and add the date to the front.
creturn = to_char( current_date, 'YYYYMMDD')::varchar || ' ' || (Select lpad( id_number::char, 7, '0' )::varchar from sequ_id where sequ_name= 'thuban_seq' )
--update the sequence table
Update sequ_id set id_number = (id_number + 1) where sequ_name= 'thuban_seq';
--return the value
return creturn ;
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.
Re: MSSQL to PostgreSQL - Issue trying to generate unique ID using actual date
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:justin@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.