Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STRING_AGG with line break

DROP TABLE IF EXISTS items;
CREATE TABLE items (item varchar(20));
INSERT INTO items VALUES ('apple'),('raspberry');
SELECT STRING_AGG(item, CHAR(13)) AS item_list FROM items;

enter image description here

How do I get a line break between items ?

like image 743
Ludovic Aubert Avatar asked Dec 11 '19 10:12

Ludovic Aubert


People also ask

How do you add a line break in SQL?

-- Using both \r\n SELECT 'First line. \r\nSecond Line. ' AS 'New Line'; -- Using both \n SELECT 'First line.

Is STRING_AGG an aggregate function?

STRING_AGG is an aggregate function that takes all expressions from rows and concatenates them into a single string. Expression values are implicitly converted to string types and then concatenated. The implicit conversion to strings follows the existing rules for data type conversions.

How do I find line breaks in SQL?

In SQL Server, we can use the CHAR function with ASCII number code. We can use the following ASCII codes in SQL Server: Char(10) – New Line / Line Break. Char(13) – Carriage Return.

How remove CR LF in SQL?

Remove and Replace Carriage Returns and Line Breaks in SQL Using SQL to remove a line feed or carriage return means using the CHAR function. A line feed is CHAR(10); a carriage return is CHAR(13).

Can you put a line break in the end of a string?

If the empty string '' is specified in end, a line break will not occur at the end. Any string can be specified in end. However, if you want to concatenate the character strings and output, it is easier to concatenate the original character strings.

How to handle line breaks (line feeds) in Python strings?

This article describes how to handle strings including line breaks (line feeds, new lines) in Python. Split a string into a list by line breaks: splitlines () Inserting a newline code , into a string will result in a line break at that location.

How to split a string by line break into a list?

The string method splitlines () can be used to split a string by line breaks into a list. In addition to and , it is also splitted by \v (line tabulation) or \f (form feed), etc. See also the following article for more information on splitlines ().

What is the result type of string_agg?

If input argument is string type ( NVARCHAR, VARCHAR ), result type will be same as input type. The following table lists automatic conversions: STRING_AGG is an aggregate function that takes all expressions from rows and concatenates them into a single string.


2 Answers

Your query is working fine on my environment. You need to enable this settings in the management studio:

enter image description here

Tools > Options > Query Results > Results to Grid

It makes no sense for me why, but they have changed the default behavior several SSMS releases ago.

like image 157
gotqn Avatar answered Oct 19 '22 12:10

gotqn


Just put it in the string:

SELECT STRING_AGG(item, '
') AS item_list
FROM items;

One caveat is that the definition of "end of line" depends on the operating system. So, this will insert a different value on Unix versus Windows.

Here is a db<>fiddle.

like image 29
Gordon Linoff Avatar answered Oct 19 '22 11:10

Gordon Linoff