2

I have a table on Sql:

ID    User       Observation
========================================
1     John       This is correct!
----------------------------------------
2     Michael    I got an error!
----------------------------------------
3     Joshua     This is incorrect!
----------------------------------------

What I want is a function thar returns a varchar With the data on a string

Like this:

Edit: This is the result I expect:

John says: This is correct!\r\nMichael says: I got an error!\r\nJoshua says: This is incorrect

Is there a way to do that?

3
  • I've tried using a cursor but I don't want to do it that way Commented Apr 19, 2012 at 20:01
  • So you want everything in a single column? Commented Apr 19, 2012 at 20:01
  • Yes I do on single column and single row Commented Apr 19, 2012 at 20:04

4 Answers 4

7

As a single column and a single row you can do this

DECLARE @out as varchar(max)

SET @Out = ''

SELECT @Out = @Out +  [User] + ' says: ' + Observation + CHAR(13) + CHAR(10)  
FROM Table1

SELECT @out

See it here

here's the output in SSMS (using text output)

-------------------------------
John says: This is correct!
Michael says: I got an error!
Joshua says: This is incorrect!

(1 row(s) affected)
Sign up to request clarification or add additional context in comments.

2 Comments

Out has many rows if the table has many rows.. I want only ONE row containing all the data.
um no it gives back one row with line breaks. that's what CHAR(13) + CHAR(10) does. Also SELECT @out can't ever return more than one row
4

If you want to concat all rows separated by a carriage-return+new-line:

declare @UserObservation Table(ID int, [User] varchar(20), Observation varchar(100));
insert into @UserObservation values(1, 'John' ,'This is correct!');
insert into @UserObservation values(2, 'Michael' ,'I got an error!');
insert into @UserObservation values(3, 'Joshua' ,'This is incorrect!');

DECLARE @WhatAllUserSay VARCHAR(8000) ;
SELECT @WhatAllUserSay = 
    COALESCE(@WhatAllUserSay + CHAR(13) + CHAR(10), '') + [User] + ' says: ' + Observation 
FROM @UserObservation

PRINT @WhatAllUserSay;

COALESCE

Output:

John says: This is correct!
Michael says: I got an error!
Joshua says: This is incorrect!

5 Comments

But this give me many rows containing strings.... what I want is a single row with all the data separating rows by a break line.
Please give an example of the EXACT output you require, then people will be able to help you (or at least tell you confidently that its not possible).
Updated... That is te result I expect
nice use of COALESCE to avoid initing the string.
COALESCE is not required in fact if you initialize your VARCHAR with ''. Great trick with ~"SELECT @WhatAllUserSay += nextUserSays FROM MultipleRowTable". I always thought that the only way to concatenate string from different rows is by using CURSOR and loop. Very very useful.
0

SELECT [User] + ' says: ' + [Observation] FROM [TableName]

Comments

0
SELECT User +' says: ' + Observation AS USER_SAID
FROM TABLE;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.