dbTalk Databases Forums  

[SQL] help needs in converting db2 function in postgresql.

mailing.database.pgsql-sql mailing.database.pgsql-sql


Discuss [SQL] help needs in converting db2 function in postgresql. in the mailing.database.pgsql-sql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Amar Dhole
 
Posts: n/a

Default [SQL] help needs in converting db2 function in postgresql. - 01-11-2011 , 06:17 AM






Hi,
I need helping converting following db2 function in postgresql function.
Any pointer will be great help in proceeding me ahead.

CREATE FUNCTION in_liststring ( string CLOB(64K) )
RETURNS TABLE ( ordinal INTEGER, index INTEGER )
LANGUAGE SQL
DETERMINISTIC
NO EXTERNAL ACTION
CONTAINS SQL
RETURN
WITH t(ordinal, index) AS
( VALUES ( 0, 0 )
UNION ALL
SELECT ordinal+1, COALESCE(NULLIF(
-- find the next delimiter ','
LOCATE(',', string, index+1), 0),
LENGTH(string)+1)
FROM t
-- to prevent a warning condition for infinite
-- recursions, we add the explicit upper
-- boundary for the "ordinal" values
WHERE ordinal < 10000 AND
-- terminate if there are no further delimiters
-- remaining
LOCATE(',', string, index+1) <> 0 )
SELECT ordinal, index
FROM t
UNION ALL
-- add indicator for the end of the string
SELECT MAX(ordinal)+1, LENGTH(string)+1
FROM t
;

commit;

DROP FUNCTION INSTRTBL;

CREATE FUNCTION INSTRTBL ( string CLOB(64K) )
RETURNS TABLE ( INSTRTBL CLOB(64K) )
LANGUAGE SQL
DETERMINISTIC
NO EXTERNAL ACTION
CONTAINS SQL
RETURN
WITH t(ordinal, index) AS
( SELECT ordinal, index
FROM TABLE ( in_liststring(string) ) AS x )
SELECT SUBSTR(string, t1.index+1, t2.index - t1.index - 1)
-- the join below makes sure that we have the lower and
-- upper index where we can find each of the ',' delimiters
-- that are separating the INSTRTBL. (For this, we exploit
-- the additional indexes pointing to the beginning and end
-- of the string.)
FROM t AS t1 JOIN t AS t2 ON
( t2.ordinal = t1.ordinal+1 )
;

--
Sent via pgsql-sql mailing list (pgsql-sql (AT) postgresql (DOT) org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql

Reply With Quote
  #2  
Old   
Filip RembiaƂkowski
 
Posts: n/a

Default Re: [SQL] help needs in converting db2 function in postgresql. - 01-11-2011 , 02:11 PM






2011/1/11 Amar Dhole <adhole (AT) tibco (DOT) com>

Quote:
Hi,
I need helping converting following db2 function in postgresql function.
Any pointer will be great help in proceeding me ahead.

CREATE FUNCTION in_liststring ( string CLOB(64K) )
RETURNS TABLE ( ordinal INTEGER, index INTEGER )
LANGUAGE SQL
DETERMINISTIC
NO EXTERNAL ACTION
CONTAINS SQL
RETURN
WITH t(ordinal, index) AS
( VALUES ( 0, 0 )
UNION ALL
SELECT ordinal+1, COALESCE(NULLIF(
-- find the next delimiter ','
LOCATE(',', string, index+1), 0),
LENGTH(string)+1)
FROM t
-- to prevent a warning condition for infinite
-- recursions, we add the explicit upper
-- boundary for the &quot;ordinal&quot; values
WHERE ordinal < 10000 AND
-- terminate if there are no further delimiters
-- remaining
LOCATE(',', string, index+1) <> 0 )
SELECT ordinal, index
FROM t
UNION ALL
-- add indicator for the end of the string
SELECT MAX(ordinal)+1, LENGTH(string)+1
FROM t
;

commit;

DROP FUNCTION INSTRTBL;

CREATE FUNCTION INSTRTBL ( string CLOB(64K) )
RETURNS TABLE ( INSTRTBL CLOB(64K) )
LANGUAGE SQL
DETERMINISTIC
NO EXTERNAL ACTION
CONTAINS SQL
RETURN
WITH t(ordinal, index) AS
( SELECT ordinal, index
FROM TABLE ( in_liststring(string) ) AS x )
SELECT SUBSTR(string, t1.index+1, t2.index - t1.index - 1)
-- the join below makes sure that we have the lower and
-- upper index where we can find each of the ',' delimiters
-- that are separating the INSTRTBL. (For this, we exploit
-- the additional indexes pointing to the beginning and end
-- of the string.)
FROM t AS t1 JOIN t AS t2 ON
( t2.ordinal = t1.ordinal+1 )
;



create or replace function instrtbl(text)
returns table(instrtbl text)
language sql
immutable
strict
as $$
SELECT * FROM regexp_split_to_table($1, ',')
$$;

filip@filip=# select * from instrtbl( 'one, two, really long three' );
instrtbl
--------------------
one
two
really long three
(3 rows)

I love PostgreSQL.

Filip

Reply With Quote
  #3  
Old   
Amar Dhole
 
Posts: n/a

Default Re: [SQL] help needs in converting db2 function in postgresql. - 01-12-2011 , 02:18 AM



Thanks this solves my problem..



________________________________

From: filip.rembialkowski (AT) gmail (DOT) com [mailto:filip.rembialkowski (AT) gmail (DOT) com] On Behalf Of Filip Rembialkowski
Sent: Wednesday, January 12, 2011 1:41 AM
To: Amar Dhole
Cc: pgsql-sql (AT) postgresql (DOT) org
Subject: Re: [SQL] help needs in converting db2 function in postgresql.





2011/1/11 Amar Dhole <adhole (AT) tibco (DOT) com>

Hi,
I need helping converting following db2 function in postgresql function.
Any pointer will be great help in proceeding me ahead.

CREATE FUNCTION in_liststring ( string CLOB(64K) )
RETURNS TABLE ( ordinal INTEGER, index INTEGER )
LANGUAGE SQL
DETERMINISTIC
NO EXTERNAL ACTION
CONTAINS SQL
RETURN
WITH t(ordinal, index) AS
( VALUES ( 0, 0 )
UNION ALL
SELECT ordinal+1, COALESCE(NULLIF(
-- find the next delimiter ','
LOCATE(',', string, index+1), 0),
LENGTH(string)+1)
FROM t
-- to prevent a warning condition for infinite
-- recursions, we add the explicit upper
-- boundary for the &quot;ordinal&quot; values
WHERE ordinal < 10000 AND
-- terminate if there are no further delimiters
-- remaining
LOCATE(',', string, index+1) <> 0 )
SELECT ordinal, index
FROM t
UNION ALL
-- add indicator for the end of the string
SELECT MAX(ordinal)+1, LENGTH(string)+1
FROM t
;

commit;

DROP FUNCTION INSTRTBL;

CREATE FUNCTION INSTRTBL ( string CLOB(64K) )
RETURNS TABLE ( INSTRTBL CLOB(64K) )
LANGUAGE SQL
DETERMINISTIC
NO EXTERNAL ACTION
CONTAINS SQL
RETURN
WITH t(ordinal, index) AS
( SELECT ordinal, index
FROM TABLE ( in_liststring(string) ) AS x )
SELECT SUBSTR(string, t1.index+1, t2.index - t1.index - 1)
-- the join below makes sure that we have the lower and
-- upper index where we can find each of the ',' delimiters
-- that are separating the INSTRTBL. (For this, we exploit
-- the additional indexes pointing to the beginning and end
-- of the string.)
FROM t AS t1 JOIN t AS t2 ON
( t2.ordinal = t1.ordinal+1 )
;




create or replace function instrtbl(text)
returns table(instrtbl text)
language sql
immutable
strict
as $$
SELECT * FROM regexp_split_to_table($1, ',')
$$;

filip@filip=# select * from instrtbl( 'one, two, really long three' );
instrtbl
--------------------
one
two
really long three
(3 rows)

I love PostgreSQL.

Filip

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.