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.
How to Insert Line Break in SQL Server String?
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.