Serverspace Black Friday
JT
November 29, 2023
Updated November 29, 2023

How to work with MySQL strings

Databases Linux

To operate with strings in MySQL within the context of an existing table containing information, you can use SQL queries which allow you to perform various operations on text fields. Let's see some examples of the most common operations performed on text data in MySQL. For example, we have a table named client that contains a column called first_name and last_name, where it stores the names of client. We consider different function and methods for work with strings!

Search data by conditions

That point very useful in the modern system with requires to retrive data by request in the mobile application, web-server or in the information system for checking critical data. But there is problem, we can know uniqueness id of the row in the table for call them data, but what if we know only data in one column? At the beginning we have dataset:

CREATE DATABASE test;
USE test;
CREATE TABLE client (id INT, first_name VARCHAR(255), last_name VARCHAR(255));
INSERT INTO client (id, first_name, last_name) VALUES (0, "Alex", "Kors"), (1, "Kork", "Ghottem"), (2, "Jorj","Fotten"), (3, "Ford", "Bold");
SELECT * FROM client;
First data
Screenshot №1 — First data

We can use context in the column operator.

SELECT * FROM client WHERE first_name= Jorj';
WHERE operator
Screenshot №2 — WHERE operator

That command will display on the screen all column in the table client where full name equal John. It's useful command commonly used for searching mechanism in the application, services and system.

Concatenate data

That function will be usefull for information system with help to fill field for user registration, format data before sending from the difference column and other significant stuff. Have a look at the example of usage. We need to concatenate first_name and last_name of user to get full_name:

SELECT CONCAT(first_name, ',  ', last_name) AS full_name FROM client;
Concatenation
Screenshot №3 — Concatenation

That command will give as the result concatenate data in the full_name, if that column don't exist, therefore they will create in the current table. By indicated sign in the query we don't need to write them every time as we want to indicate new column.

Highlight! You can notice, that we indicate operators in the CAPITAL letters, but also we can use another case, but name of the database, column and other objects case-sensitive! For example, we can write Select, but can't write Online_Store instead of original value online_store!

 Select Substring

There are situations when we need to select part of text value and show data in the application more shorter or due to reason limitations in another system we want to fit into parameters. For that we can imagine case when we need to get first letter of name from our customers. We will use function SUBSTRING, when we need indicate column in the table, first number of sign and quantity of letters:

SELECT SUBSTRING(first_name,1,1) FROM client;

Don't forget semicolon at the end of query! Let's check result of that command:

Substring
Screenshot №4 — Substring

Also we can add operator AS and then we get result in the table as we described above!

SELECT SUBSTRING(first_name,1,1) AS first_letter FROM client;

That you can use for application, more fit usage in the different contex!

UPPER & LOWER

That function will be useful for purposes to format input data from client for reason needs unified. Therefore, function UPPER make letter capital case and LOWER in the logical sequences lower case:

SELECT UPPER (first_name) FROM client;
Upper
Screenshot №5 — Upper

And lower case therefore:

SELECT LOWER (first_name) FROM client;

And the last of commonly used function we will consider REPLACE!

Replace

That function will help change data for any reason you have. Syntax is simplify, at firts attribute function you indicate column, reference text and target text: REPLACE (fist_name,'reference_text','target_text');

SELECT REPLACE(first_name,'Jorj','Huston') FROM client;
Replace
Screenshot №6 — Replace

We consider main function for string and their values, all of that could you help in the different cases and situations!

Conclusion

Understanding and utilizing these string functions enhances the versatility of MySQL in handling text data. Whether for searching, concatenating, substring extraction, case conversion, or data replacement, these operations empower developers and database administrators to efficiently manage and manipulate textual information within their databases.

Vote:
5 out of 5
Аverage rating : 5
Rated by: 1
33145 North Miami, FL 2520 Coral Way apt 2-135
+1 302 425-97-76
700 300
ITGLOBAL.COM CORP
700 300

You might also like...

We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.