Click here to Skip to main content
12,350,035 members (24,180 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

5.1K views
4 bookmarked
Posted

SQL function to return CSV as a table

, 12 May 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
/*************...
/******************************************************************************
Function Name   : GetMyTable
Purpose         : Returns CSV as a table with each value in a row.
Execute Format  : select * from dbo.GetMyTable('1,12,14')
*******************************************************************************/
 
ALTER    FUNCTION [dbo].[GetMyTable]
(
    @CSV varchar(5000)
)
RETURNS @MyTable table
(
    VALUE varchar(5000)
)
as
begin
    while(charindex(',',@CSV,0) > 0)
    begin
        declare @FTEMP varchar(2000)
        set @FTEMP = substring(@CSV,0,charindex(',',@CSV))
        set @CSV = substring(@CSV,len(@FTEMP) + 2, len(@CSV))
        insert into @MyTable
        select @FTEMP
    end
    insert into @MyTable select @CSV
    return
end

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Neh.C
Software Developer (Senior)
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 12 May 2010
Article Copyright 2010 by Neh.C
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid