How to Insert Line Break in SQL Server String?
Last Updated :
02 Feb, 2024
In SQL Server there are various datatypes like int, float, char, nchar, etc but especially while we are dealing with text in VARCHAR and NVARCHAR columns, we might run into situations where we need to make the text look cleaner by adding line breaks. This could be for better organization, and readability, data for display, or working with flat files, excel, or other applications.
In this article, we will break down simple methods to easily insert line breaks into our VARCHAR and NVARCHAR strings in SQL Server. Whether we are new to SQL or have some experience, these techniques will help us to improve the way to look at our data and make it more user-friendly.
Introduction to Insert Line Break in SQL Server String
We can solve our problem using CHAR (integer_expression). It is used to insert control characters into the string. CHAR(10) and CHAR(13) are used to insert Line Feed(LF) and Carriage Return(CR) respectively.
Before going to implementation and example we will understand what is Line Feed and Carriage Return.
- Line Feed: It means moving one line forward. Its code representation is \n.
- Carriage Return: It means moving the cursor to the beginning of the line. Its code representation is \r.
In this article, we are going to see the use of both.
Syntax:
SET @var = @string1 + CHAR(10) + @string2
OR
SET @var = @string1 + CHAR(13) + @string2
So, let's define a varchar string and split it into multiple parts.
Examples of How to Insert Line Break in SQL Server String
Example 1: Let's Declares a Variable @myString
of Type VARCHAR(100) and Sets Some value to it
Let's Create a SQL script that declares a variable @myString
of type VARCHAR(100) and sets its value to 'This is article. It is on GeeksForGeeks. This is SplitString'. Finally, select and display the value of @myString
as 'myString'.
Query:
DECLARE @myString as VARCHAR(100)
SET @myString = 'This is article. It is on GeeksForGeeks. This is SplitString'
SELECT @myString as myString
Output:
This is the output of the SQL. We can see the output in the single lineExplanation: We have successfully declared a variable @myString of
VARCHAR(100) and set
strings.
Example 2: Let's Store Multi-Sentence String with Line Breaks Within a SQL Server
Let's add CHAR(10) and see how it works.
Suppose we need to temporarily store a multi-sentence string with line breaks within a SQL Server query. This string contains three sentences which are:
- This is article.
- This is on GeeksForGeeks.
- This is SplitString
Query:
DECLARE @myString AS VARCHAR(100)
SET @myString = 'This is article.' + CHAR(10) + 'This is on GeeksForGeeks.' + CHAR(10) + 'This is SplitString'
SELECT @myString as myString
Output:
Tada! We have broken down our string into 4 substrings. SQL Server treats it as \n character or line breakExplanation: In the above example, in results, we can see that our string variable @myString is broken into multiple lines. So by using CHAR(10), line break is added into the varchar/nvarchar string. Please keep one thing mind that integer "10" means Line Feed in ASCII. More line breaks we need, more CHAR(10) can be added into the string.
Example 3: Let's Store a Multi-Line String
Let Suppose we want to store a multi-line string that appears as:
- This is article.
- It is on GeeksForGeeks.
- This is SplitString
in a SQL Server variable named @myString
and then retrieve it.
Query:
DECLARE @myString AS VARCHAR(100)
SET @myString = 'This is article.' + CHAR(13) + 'It is on GeeksForGeeks.' + CHAR(13) + 'This is SplitString'
SELECT @myString as myString
Output:
Output is same as above example but /r code is used behind the scene by SQL ServerExplanation: In this example, we can that line break is added and there are multi strings. So by using CHAR(13), line break is added into the varchar/nvarchar string. Please keep one thing mind that integer "13" means Carriage return in ASCII.
Conclusion
Throughout this article, we have understand the methods for breaking or splitting lines using CHAR(10) and CHAR(13). To precisely introduce a line break in the string, we have utilized CHAR(10), which functions as the equivalent of \n for SQL Server. After reading whole article now you have good understanding about how to insert line breaks in SQL Server through various implementations and examples.
Similar Reads
How to insert a line break in a String PL/SQL
In PL/SQL, inserting line breaks into VARCHAR or NVARCHAR strings can be a good way to enhance the readability and presentation of our data. Whether we are formatting output for display or preparing text for storage, knowing how to insert line breaks is a helpful skill. Here, we will explore techniq
5 min read
How to Insert a Line Break in a String PostgreSQL ?
In PostgreSQL, managing strings efficiently is crucial, especially when dealing with VARCHAR and NVARCHAR data types. One common challenge developers face is inserting line breaks within these strings. In this article, we'll explore different approaches to tackle this issue, allowing us to format ou
5 min read
How to insert a line break in PHP string ?
In this article, we will discuss how to insert a line break in a PHP string. We will get it by using the nl2br() function. This function is used to give a new line break wherever '\n' is placed. Syntax: nl2br("string \n");where the string is the input string. Example 1: PHP Program to insert a line
2 min read
How to Insert a Line Break VARCHAR String in MySQL
When dealing with textual data in MySQL databases, maintaining proper formatting is important for readability and clarity. One common formatting requirement is to insert line breaks within VARCHAR and NVARCHAR strings. In this article, we will understand How to insert a line break in a MySQL VARCHAR
3 min read
How to Insert a Line Break in a SQLite VARCHAR String
SQLite is a popular relational database management system known for its simplicity and flexibility. However, inserting a line break in a VARCHAR/NVARCHAR string can be challenging due to SQLite's limited support for special characters. In this article, we will explore different approaches to inserti
3 min read
How to Insert a Line Break in a SQL VARCHAR
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 characte
3 min read
How to UPDATE and REPLACE Part of a String in SQL Server
In SQLServer, efficient manipulation of strings is crucial for managing databases effectively. Among the fundamental operations are updating and replacing parts of strings within tables. These operations are invaluable for correcting data inconsistencies, enhancing data quality, and transforming tex
4 min read
How to Insert Line Breaks in Google Sheets: Step-by-Step Guide
Inserting line breaks in Google Sheets can greatly enhance your data's readability and organization, especially when working with lengthy text entries. Whether you're formatting addresses, lists, or notes, knowing how to insert a line break using shortcuts or formulas is essential. This article will
6 min read
How to create multi-line strings in JavaScript ?
In JavaScript, the absence of native support for multi-line strings was a longstanding challenge. However, with the advent of ES6 (ECMAScript 2015), the things changed dramatically. ES6 introduced string literals, offering robust support for multi-line strings, which was a significant enhancement fo
3 min read
Insert Statement in MS SQL Server
The SQL Server INSERT statement is a fundamental command used to add new rows of data to a table. Whether we are inserting specific values, utilizing default values or copying data from another table. In this guide, weâll explore various ways to use the Insert statement in MS SQL Server with the hel
4 min read