Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2015 May MySQL String Functions

MySQL String Functions

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing few essential string manipulation functions of MySQL.

Below is a list of String functions with an example.

CONCAT (Str1,Str2…):This function is used to concat two or more string and integer by passing a string or integer arguments. If you try to concat NULL with string, the result will be NULL. You can also concat multiple arguments using a single function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
mysql> SELECT CONCAT('String',' Function') AS CONCAT;
+-----------------+
| CONCAT |
+-----------------+
| String Function |
+-----------------+
 
mysql> SELECT CONCAT('String',' Function',' Demo') AS CONCAT;
+----------------------+
| CONCAT |
+----------------------+
| String Function Demo |
+----------------------+
 
mysql> SELECT CONCAT('String',' Function',null) AS CONCAT;
+--------+
| CONCAT |
+--------+
| NULL |
+--------+
 
mysql> SELECT CONCAT('String',' Function ',10) AS CONCAT;
+--------------------+
| CONCAT |
+--------------------+
| String Function 10 |
+--------------------+

LEFT (str,len): This will return left most character from string and if you pass NULL argument, returns NULL result.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mysql> SELECT LEFT ('String Function',5);
 
+----------------------------+
| LEFT ('String Function',5) |
+----------------------------+
| Strin |
+----------------------------+
 
mysql> SELECT LEFT (15245,3);
+----------------+
| LEFT (15245,3) |
+----------------+
| 152 |
+----------------+

RIGHT (str,len) : This will return right most character from string and if you pass NULL argument, returns NULL result.

1
2
3
4
5
6
7
mysql> SELECT RIGHT('String Function',5);
 
+----------------------------+
| RIGHT('String Function',5) |
+----------------------------+
| ction |
+----------------------------+

INSTR (str,substr) : This function is used to find starting position of given substring.

1
2
3
4
5
6
mysql> SELECT INSTR('String Function','Fun');
+--------------------------------+
| INSTR('String Function','Fun') |
+--------------------------------+
| 8 |
+--------------------------------+

LOCATE (substr,str) : This function work same as INSTR(str,substr)

1
2
3
4
5
6
mysql> SELECT LOCATE('Fun','String Function');
+---------------------------------+
| LOCATE('Fun','String Function') |
+---------------------------------+
| 8 |
+---------------------------------+

POSITION (substr IN str) : This function work same as INSTR(str,substr)

1
2
3
4
5
6
mysql> SELECT POSITION('Fun' IN 'String Function');
+--------------------------------------+
| POSITION('Fun' IN 'String Function') |
+--------------------------------------+
| 8 |
+--------------------------------------+

INSERT(str,pos,len,newstr) : This function is used to overwrite new string part on an old string part.
You require passing position and length to replace old part of string using the new part of a string.

1
2
3
4
5
6
mysql> SELECT INSERT('String Function',7,4,'Demo');
+--------------------------------------+
| INSERT('String Function',7,4,'Demo') |
+--------------------------------------+
| StringDemoction |
+--------------------------------------+

LENGTH(str): This function is used to find length of given string.

1
2
3
4
5
6
mysql> SELECT LENGTH('String Function');
+---------------------------+
| LENGTH('String Function') |
+---------------------------+
| 15 |
+---------------------------+

UPPER(str) / LOWER(str) : This both function is used to change string from lower to upper or upper to lowar case.

1
2
3
4
5
6
7
8
9
10
11
12
13
mysql> SELECT LOWER('FUNCTION');
+-------------------+
| LOWER('FUNCTION') |
+-------------------+
| function |
+-------------------+
 
mysql> SELECT UPPER('function');
+-------------------+
| UPPER('function') |
+-------------------+
| FUNCTION |
+-------------------+

REPEAT (str,count): This function is used to repeat string by given count.

1
2
3
4
5
6
mysql> SELECT REPEAT(' String Function',3);
+-----------------------------------------------------+
| REPEAT(' String Function',3) |
+-----------------------------------------------------+
| String Function String Function String Function |
+-----------------------------------------------------+

REPLACE(str,from_str,to_str): This function is used to replace a substring by the new string. You can replace any part of string by giving from_string and to_string.

1
2
3
4
5
6
mysql> SELECT REPLACE('String Function','Function','Demo');
+----------------------------------------------+
| REPLACE('String Function','Function','Demo') |
+----------------------------------------------+
| String Demo |
+----------------------------------------------+

REVERSE(str) : This function is used to reverse each word of string.

1
2
3
4
5
6
mysql> SELECT REVERSE ('String Function');
+-----------------------------+
| REVERSE ('String Function') |
+-----------------------------+
| noitcnuF gnirtS |
+-----------------------------+

STRCMP(str1,str2): This function is used to compare two string. This is not case sensitive. If both strings match, then it returns 0 else it returns 1 or -1 base on how first string is bigger than the second string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
mysql> SELECT STRCMP('String','String');
+---------------------------+
| STRCMP('String','String') |
+---------------------------+
| 0 |
+---------------------------+
 
mysql> SELECT STRCMP('STRING','string');
+---------------------------+
| STRCMP('STRING','string') |
+---------------------------+
| 0 |
+---------------------------+
 
mysql> SELECT STRCMP('STRING','Function');
+-----------------------------+
| STRCMP('STRING','Function') |
+-----------------------------+
| 1 |
+-----------------------------+
 
mysql> SELECT STRCMP('String', 'tring');
+---------------------------+
| STRCMP('String', 'tring') |
+---------------------------+
| -1 |
+---------------------------+

SUBSTRING (str,pos) : This function is used to return sub part of string by giving position.

1
2
3
4
5
6
mysql> SELECT SUBSTRING('String Function',5);
+--------------------------------+
| SUBSTRING('String Function',5) |
+--------------------------------+
| ng Function |
+--------------------------------+

SUBSTRING (str,pos,len): This function is used to return sub part of string by giving position and length of return string.

1
2
3
4
5
6
mysql> SELECT SUBSTRING('String Function',5,8);
+----------------------------------+
| SUBSTRING('String Function',5,8) |
+----------------------------------+
| ng Funct |
+----------------------------------+

TRIM (str): This function is used to remove all prefixes or suffixes from string. You can also use LTRIM(str) and RTRIM(str) to remove space from the left or right only.

1
2
3
4
5
6
mysql> SELECT TRIM(' Test ');
+----------------+
| TRIM(' Test ') |
+----------------+
| Test |
+----------------+

Above all are basic and important String manipulation function in MySQL.

May 19, 2015Anvesh Patel
Insert script for all CountriesEffective storage of Email Address into Database System
Comments: 1
  1. Alexandr Omelchenko
    March 25, 2016 at 10:55 am

    Thanks! Here I have found all basic and important String Manipulation Function for MySql.
    By the way, we have created a cheat sheet – “MySQL String Functions” with the whole list of MySql String Functions. You can find it here:
    http://mysqlbackupftp.com/freebies/MySQL_Cheat_Sheet_String_Functions.pdf

Anvesh Patel
Anvesh Patel

Database Engineer

May 19, 2015 MySQLAnvesh Patel, database, database research and development, MySQL, MySQL Command, MySQL Database Administrator, MySQL Database Programming, MySQL Query, String Example., String Functions, String Parsing
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....