How to Insert a Line Break VARCHAR String in MySQL
Last Updated :
05 Feb, 2024
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/NVARCHAR string using two data types: VARCHAR (Variable Character) and NVARCHAR (National Variable Character).
Introduction to VARCHAR/NVARCHAR String in MySQL
In MySQL, VARCHAR (Variable Character) is a data type that allows us to store variable-length character strings. The length of a VARCHAR column is variable and can hold up to a specified maximum length. On the other hand, NVARCHAR (National Variable Character) is also a variable-length string data type, but it is used for character strings in the National Language Character Set.
Insert a line break in a MySQL VARCHAR/NVARCHAR string
Let's see some examples of How to insert a line break in a MySQL VARCHAR/NVARCHAR string.
Example 1: Using the CONCAT Function
In this example, the CONCAT function is used to concatenate the desired text with a line break. The CHAR(13) and CHAR(10) represent the ASCII characters for carriage return and line feed, respectively. Together, they create a line break in the string.
Query:
-- Create a sample table
CREATE TABLE GeeksforGeeks
(
id INT PRIMARY KEY,
text_data VARCHAR(255)
);
-- Insert a record with a line break
INSERT INTO GeeksforGeeks (id, text_data)
VALUES (1, CONCAT('First line', CHAR(13),
CHAR(10), 'Second line'));
-- Retrieve the data
SELECT * FROM GeeksforGeeks;
Output:

Explanation: Output of the code creates a table named GeeksforGeeks with columns id and text_data. It inserts a record with id = 1 and text_data containing two lines separated by a line break. The output will show the inserted record with a line break between "First line" and "Second line.
Example 2: Using the CONCAT_WS Function
In this example, the CONCAT_WS (Concatenate With Separator) function is used to concatenate the specified strings with an empty separator. This effectively combines the strings into one, creating a line break between them.
Query:
-- Create a sample table
CREATE TABLE GeeksforGeeks(
id INT PRIMARY KEY,
text_data VARCHAR(255)
);
-- Insert a record with a line break
INSERT INTO GeeksforGeeks (id, text_data)
VALUES (1, CONCAT_WS('\n', 'First line', 'Second line'));
-- Retrieve the data
SELECT * FROM GeeksforGeeks;
Output :

Explanation: Output of the code creates a table named GeeksforGeeks with columns id and text_data. It inserts a record with id = 1 and text_data containing two lines separated by a line break. The output will show the inserted record with a line break between "First line" and "Second line.
Conclusion
Effectively inserting line breaks in MySQL VARCHAR/NVARCHAR strings is important for maintaining data clarity. By using functions like CONCAT and CONCAT_WS, you can easily concatenate text and special characters resulting in a well-formatted and readable format. The choice between these methods depends on the specific formatting needs of our data.
Similar Reads
SQL Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases.In this S
11 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
ACID Properties in DBMS In the world of DBMS, transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliability. This is where the ACID prop
8 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read