Skip to content

Instantly share code, notes, and snippets.

@Isxida
Created November 2, 2012 21:10
Show Gist options
  • Save Isxida/4004347 to your computer and use it in GitHub Desktop.
Save Isxida/4004347 to your computer and use it in GitHub Desktop.
mySQL function
#MySQL
ALTER FUNCTION [dbo].[fn_gen_listas] (@chr_lista VARCHAR(4096))
RETURNS @tbl_lista TABLE
(
valor VARCHAR(1024)
)
AS
BEGIN
DECLARE @chr_Item VARCHAR(120)
DECLARE @separador CHAR(1)
IF @chr_lista = '*'
SET @chr_lista = ''
IF @chr_lista = ''
RETURN
IF @separador IS NULL
SET @separador = ','
IF SUBSTRING(@chr_lista, LEN(@chr_lista), 1) <> @separador
SET @chr_lista = @chr_lista + @separador
WHILE CHARINDEX(@separador,@chr_lista,0) <> 0
BEGIN
SELECT
@chr_Item=RTRIM(LTRIM(SUBSTRING(@chr_lista,1,CHARINDEX(',',@chr_lista,0)-1))),
@chr_lista=RTRIM(LTRIM(SUBSTRING(@chr_lista,CHARINDEX(',',@chr_lista,0)+1,LEN(@chr_lista))))
IF LEN(@chr_Item) > 0
INSERT INTO @tbl_lista SELECT @chr_Item
END
RETURN
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment