Results 1 to 2 of 2

Thread: sql server put a column into a nvarchar

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Posts
    407

    sql server put a column into a nvarchar

    HI Guys,
    I'm trying to take a ms sql server 2008 column and merge it into a nvarchar. So instead of being a column it will be come a file. Does anyone know how to do that in a stored procedure?

    Say you have a table with a column "Names"
    john
    mary
    jack
    joe

    How do you merge all these cells into a single string so now it would be:
    "john mary jack joe"

    thanks.
    My Websites
    SharpMP3 - MP3 Design Articles www.sharpmp3.com
    Yobbers - Job Search www.yobbers.com
    Lets Trend - Methods For Riding Stock Trends www.letstrend.com

  2. #2
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: sql server put a column into a nvarchar

    you can use a cursor to achieve this...but nvarchar datatype has a limitation of 4000 char..
    Code:
    DECLARE @Name as nvarchar(64)
    DECLARE @RowToCol as nvarchar(4000)
    SET @RowToCol = ''
    DECLARE Employee_Cursor CURSOR FOR
    SELECT Emp_Name FROM Employee
    OPEN Employee_Cursor
    FETCH NEXT FROM Employee_Cursor
    WHILE @@FETCH_STATUS = 0
    BEGIN
       FETCH NEXT FROM Employee_Cursor INTO @Name
       SET 	@RowToCol = @RowToCol + ' ' + @Name
    END
    CLOSE Employee_Cursor
    DEALLOCATE Employee_Cursor
    
    SELECT @RowToCol
    __________________
    Rate the posts that helped you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width