You must Sign In to post a response.
  • Category: SQL Server

    How to use PatIndex in the Sql Server for Comma Separated Values

    CREATE TABLE tblPatIndex
    (
    USER_ID int identity(1,1) Primary Key,
    Friend_ID nvarchar(250)
    )

    insert into tblPatIndex
    (Friend_ID)
    values('1,2,4,5,6,77,61, 8, 9, 99, 98, 96, 97, 96')

    --Here I want comma separted values from the Friend_ID Column
  • #599994
    hi ,

    try this


    declare @str varchar(200)
    declare @chr char(1)
    declare @val numeric
    declare @cnt numeric
    set @cnt = 0
    set @str = '1,2,4,5,6,77,61, 8, 9, 99, 98, 96, 97, 96'
    if right(@str,1)<>','
    begin
    set @str = @str+','
    end
    set @val = (len(@str) - len(replace(@str,',','')))
    while @cnt < @val
    begin
    print substring(@str,1,patindex('%,%',@str)- 1)
    set @str = substring(@str,patindex('%,%',@str) + 1,len(@str) - patindex('%,%',@str))
    set @cnt = @cnt + 1
    end



    jeebu


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.