Below is the function return in SQL
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE FUNCTION [dbo].[Lk_Split](@String varchar(MAX), @Delimiter char(1))
returns @temptable TABLE (items varchar(MAX), rn int)
as
begin
declare @idx int
declare @slice varchar(8000)
declare @rn int = 1 -- row number that increments with each value in the delimited string
select @idx = 1
if len(@String)<1 or @String is null return
while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
if(len(@slice)>0)
insert into @temptable(Items, rn) values(@slice, @rn)
set @String = right(@String,len(@String) - @idx)
set @rn = @rn +1
if len(@String) = 0 break
end
return
end
GO
I need to convert this function to Postgress database to work it in that DB.
What I have tried:
I have tried the below
CREATE FUNCTION LK_Split(String varchar(5000), Delimiter char(1))
returns table
language plpgsql
as
$$
declare
idx integer;
slice varchar(8000);
table1 table;
select idx = 1
if length(String)<1 or String is null return
while idx!= 0
begin
set idx = charindex(Delimiter,String);
if idx!=0
set slice = left(String,idx - 1;
else
set slice = String ;
if(length(slice)>0)
select values(slice) into temptable(Items) from table1
return table1;
set String = right(String,length(String) - idx)
if length(String) = 0 break ;
end;
end;
$$;
But it is not working due to return of table value.