Popular string functions in MySQL – CONCAT and SUBSTRING

There are some very helpful and fun string functions in MySQl and I’ve decided to review them here as I go through Colt Steele’s MySQL Udemy course. This article will serve as my notes for that section of the course.

CONCAT

The CONCAT func…


This content originally appeared on DEV Community and was authored by Jo

There are some very helpful and fun string functions in MySQl and I've decided to review them here as I go through Colt Steele's MySQL Udemy course. This article will serve as my notes for that section of the course.

CONCAT

The CONCAT function concatenates two or more value together. This is helpful for formatting data that is useful to the person reading the output.

select concat(author_fname, " ", author_lname, " - ", title) AS Author
from books;

A variation of the CONCAT function is the CONCAT_WS function, which stands for CONCAT with separator. This is useful if you're separating multiple pieces of data with the same symbol between them. The first argument in the CONCAT_WS function is the operator used to separate all the the other individual arguments.

select concat_ws(' - ', author_fname, author_lname, title) 
AS Author
from books;

As you can see from the example above, there is now a dash between the author's first name, last name, and book title.

SUBSTRING

Another helpful function is substring. This function is useful for extrapolating data when you only need a part of the string. This function is also useful in combination with other functions to make even more powerful queries. The basic makeup of a simple substring query includes the string that data needs to be extrapolated from, and the location of the data.

select substring('Jowayne', 1,2)

The result of the example above would give me "Jo", as the Jowayne is the string to be analyzed, the 1 is location where I need the extrapolation to begin, and 2 is where I need it to end.
You can also choose to just use one number(the starting index) and the function will automatically start at that number and go till the end, like so:

select substring('Jowayne',3)

The result of this would be "wayne" since I started at 3.
The substring function also accommodates negative numbers. The substring would begin at the end of the string so:

select substring('Jowayne',3)

The result would be 'yne'.

And as I referenced before, you can use substring with other string functions:

select concat(substring(title, 1, 10),'...') from books;

That's it for this post! Thanks for reading :)


This content originally appeared on DEV Community and was authored by Jo


Print Share Comment Cite Upload Translate Updates
APA

Jo | Sciencx (2021-04-23T16:37:06+00:00) Popular string functions in MySQL – CONCAT and SUBSTRING. Retrieved from https://www.scien.cx/2021/04/23/popular-string-functions-in-mysql-concat-and-substring/

MLA
" » Popular string functions in MySQL – CONCAT and SUBSTRING." Jo | Sciencx - Friday April 23, 2021, https://www.scien.cx/2021/04/23/popular-string-functions-in-mysql-concat-and-substring/
HARVARD
Jo | Sciencx Friday April 23, 2021 » Popular string functions in MySQL – CONCAT and SUBSTRING., viewed ,<https://www.scien.cx/2021/04/23/popular-string-functions-in-mysql-concat-and-substring/>
VANCOUVER
Jo | Sciencx - » Popular string functions in MySQL – CONCAT and SUBSTRING. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/23/popular-string-functions-in-mysql-concat-and-substring/
CHICAGO
" » Popular string functions in MySQL – CONCAT and SUBSTRING." Jo | Sciencx - Accessed . https://www.scien.cx/2021/04/23/popular-string-functions-in-mysql-concat-and-substring/
IEEE
" » Popular string functions in MySQL – CONCAT and SUBSTRING." Jo | Sciencx [Online]. Available: https://www.scien.cx/2021/04/23/popular-string-functions-in-mysql-concat-and-substring/. [Accessed: ]
rf:citation
» Popular string functions in MySQL – CONCAT and SUBSTRING | Jo | Sciencx | https://www.scien.cx/2021/04/23/popular-string-functions-in-mysql-concat-and-substring/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.