Open In App

How to Insert a Line Break in a SQL VARCHAR

Last Updated : 05 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In SQL, VARCHAR and NVARCHAR are widely used data types for storing character data. It may sometimes be necessary to insert line breaks into these string values ​​for better readability or to display the information in a formatted manner. Although SQL itself does not have a specific newline character, there are methods to achieve this.

In this article, we will explore different approaches to inserting line breaks into VARCHAR and NVARCHAR SQL strings.

Enhancing Readability in SQL VARCHAR/NVARCHAR Strings with Line Breaks

In SQL, inserting a line break into a VARCHAR or NVARCHAR string can be achieved using functions like CHAR or CONCAT. This is particularly useful for formatting text or creating more readable output.

Prerequisites

Before going to line breaks, let us first understand the difference between VARCHAR and NVARCHAR.

VARCHAR (Variable Character):

  1. Supports a specific character set (e.g., ASCII, UTF-8).
  2. Stores characters without regard to Unicode requirements, potentially more space-efficient.
  3. Ideal for monolingual scenarios where storage optimization is critical.

NVARCHAR (National Variable Character):

  1. Supports the Unicode character set (UTF-16), offering broader language support.
  2. Uses a two (or more) bytes per character storage mechanism to accommodate Unicode characters, resulting in larger storage requirements.
  3. Essential when dealing with data in multiple languages or when Unicode character support is necessary.

Understanding Line Break Characters

Before diving in, it’s important to understand the different characters that represent line breaks. The most common are:

1. Line Feed (LF): Represented by CHAR(10) or \n, is a character or escape sequence that instructs a text cursor to move down one line in a document or text file without changing its horizontal position.

2. Carriage return (CR): Indicated by CHAR(13) or \r, moves the cursor to the beginning of the current line.

3. Carriage return + Line feed (CRLF): Represented by CHAR(13) + CHAR(10) or \r\n, the most widely used combination to add line breaks independent of the platform.

Various approaches for line breaks

1. Direct Concatenation

This involves directly appending the desired line break character(s) to your string. For example:

DECLARE @String VARCHAR(100)
SET @String = ' Hello, This is line 1.' + CHAR(10) + ' Hi, This is line 2.'
SELECT @String AS Result;

Output:

Line-Break-usinf-LF

Line break using LF

Note: Ensure that the ‘N’ prefix is used with string literals for NVARCHAR data types as shown below.

Explanation: The output is a single string variable with two lines. It contains the concatenated text “Hello, This is line 1.” on the first line and “Hi, This is line 2.” on the second line, separated by a newline character (CHAR(10)).

2. Using the CONCAT Function

DECLARE @NString NVARCHAR(100)
SET @NString = CONCAT(N'Example of NVARCHAR.', NCHAR(10) + NCHAR(13), N'By Concat.')
SELECT @NString AS Result;

Output:

Line-break-by-CRLF

Line break by CRLF

Explanation: The output is an NVARCHAR variable, @NString, containing concatenated text. It combines “Example of NVARCHAR.” with a new line (NCHAR(10)) and carriage return (NCHAR(13)), followed by “By Concat.” The resulting string demonstrates line breaks in NVARCHAR format.

3. Using the REPLACE Function

DECLARE @text NVARCHAR(MAX) = 'This is <br> line 1.';

SELECT REPLACE(@text, '<br>', CHAR(13) + CHAR(10));

Output:

CRLF-using-replace

Line Break by CRLF using replace

Explanation: The output is the variable @text with the HTML-like tag ‘<br>‘ replaced by a carriage return (CHAR(13)) and a line feed (CHAR(10)). It transforms “This is <br> line 1.” into “This is \r\n line 1.” to represent line breaks.

Conclusion

  • There are other approaches, such as the FOR XML PATH function, STUFF function etc. Select the approach that best fits the situation and your needs.
  • If you use line break characters, take platform compatibility into consideration.
  • Although NVARCHAR supports a larger range of characters than VARCHAR, it needs twice as much storage space.
  • Recall that while the cursor action may not always visually indicate line breaks, the formatting information is still retained in the data itself.

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads