In this tutorial, I am going to explain about MySQL String Functions with examples. This article will cover the following topics. Let’s see.
- Introduction
- String Functions in MySQL
- Conclusion
STRING FUNCTIONS IN MYSQL
This function returns ASCII code value of the leftmost character of the String “str” and returns 0 if the “str” is the empty string. And, it returns NULL if “str” is NULL.
Syntax
ASCII (str)
Example
- SELECT ASCII('0'),
- ASCII('A'),
- ASCII('a'),
- ASCII('Onkar'),
- ASCII(''),
- ASCII(NULL);
In the BIT_LENGTH(str) function return the String str length in bit forms.
Syntax
BIT_LENGTH (str);
Example
- SELECT BIT_LENGTH('a'),
- BIT_LENGTH('VATSA'),
- BIT_LENGTH('Hello!Vatsa');
CHAR (N,... [USING charset_name] ) Function returns a string consisting of the character and the given integer value. This function skipped the NULL values.
Syntax
CHAR (N, [USING charset_name] );
Example
- SELECT CHAR(77,121,83,81,'76');
The CONCAT(str1, str2?.) function can have one or more arguments and it returns a string that is the result of concatenating the arguments. In this function if all arguments are non-binary strings, then the result is also non-binary string but if any argument is a binary string then the result is a binary string. And a numeric argument is converted to its equivalent binary string form. But if any argument is NULL then it also returns NULL.
Syntax
CONCAT (str1, str2...);
Example
- SELECT CONCAT('In', 'd', 'ia') AS Name,
- CONCAT('V', 'A', 'T', 'S', 'A') as Name,
- CONCAT('my', NULL, 'ql') as Name,
- CONCAT(10, 3) as String;
CONCAT_WS () means CONCAT with Separator. The first argument is treated as a separator for the
rest of the arguments and it is added between the strings for concatenating. If the separator is NULL then the result is NULL.
Syntax
CONCAT_WS (separator str1, str2 ...);
Example
- SELECT CONCAT_WS(',', 'Title', 'First name', 'Last Name'),
- CONCAT_WS(',', 'First name', NULL, 'Last Name');
The FIELD(str,str1,str2,str3,....) function is used to find the index position of str in the arguments str1,str2,str3. In other words it returns the index position of str in the arguments. It returns 0 if str is not available in the arguments. If str is NULL then return value is 0 because NULL fails equality comparison with any value.
Syntax
FIELD (str, str1,str2, str3, ...);
Example
- SELECT FIELD ('AA', 'BB', 'AA', 'CC');
FIND_IN_SET (str,strlist) function returns a value in the range of 1 to N. This function finds the String str in the substring of String list strlist” and returns the index value. This String list has many substrings that are separated by “,” Characters.
This function returns 0 when “str” is not available in “strlist” or “strlist” is the empty string.
Syntax
FIND_IN_SET (str,strlist);
Example
- SELECT FIND_IN_SET('2', '1,2,3,4'),
- FIND_IN_SET('7', '14,21,28,35'),
- FIND_IN_SET('2', ''),
- FIND_IN_SET('2', '1,2,3,4');
In the FORMAT(X, D) function format, the number X is rounded to the decimal places to D then returns the string as a result. But if D is 0 then, the results don’t have fractional part.
Syntax
FORMAT (X, D);
Example
- SELECT FORMAT(1235.14687, 5),
- FORMAT(1235.14687, 4),
- FORMAT(1235.14687, 3),
- FORMAT(1235.14687, 2),
- FORMAT(1235.14687, 1);
This function is used to replace some part or whole String of String “str” with String “newstr” from beginning at position pos and “len” character long. This function returns the String “str” if pos is not within the length of the string. It returns NULL if any argument is NULL.
Syntax
INSERT (str, pos,len, newstr);
Example
- SELECT INSERT('h***oindia', 2, 3, 'ell') AS STRING,
- INSERT('helloindia', -1, 3, 'net') AS STRING;
INSTR
This function is used to return the position of first occurrence of “substr” in “str” String.
Syntax
INSTR (str, substr)
Example
- SELECT INSTR('VATSA', 'A'),
- INSTR('VATSA', 'O'),
- INSTR('VATSA', NULL);
The LOWER (str) function returns the String “str”. And, in this String all the characters are changed in lowercase.
Syntax
LOWER (str)
Example
- SELECT LOWER('HELLO!INDIA...'),
- LOWER('VATSA'),
- LOWER('Hello!Vatsa...');
This function returns the leftmost “len” characters from the String “str”.
Syntax
LEFT (str, len);
Example
- SELECT LEFT('hello!india', 6),
- LEFT('Welcome', 3),
- LEFT('Sunset', 3);
The LENGTH (str) function returns the length of the String “str” in bytes.
Syntax
LENGTH(str)
Example
- SELECT LENGTH("HelloIndia"),
- LENGTH("VATSA"),
- LENGTH("hello!india...");
LTRIM
LTRIM (str) function is used to return the string “str” with leads in the space characters to be removed.
Syntax
LTRIM (str);
Example
- SELECT LTRIM(' helloindia') AS String,
- LTRIM(' VATSA ') AS String,
- LTRIM('H E L L O ! V A T S A...') AS String;
REPEAT (str, count) function returns a string that consist a String “str” repeated of count times. But, if count time is less than 1 than it returns an empty string.
Syntax
REPEAT (str,count);
Example
- SELECT REPEAT('VATSA,...', 11);
In REPLACE (str,from_str, to_str) function, it returns the String “str” and in this String all occurrences of the String “from_str” are replaced by the String “to_str”. This function can perform a case-sensitive match when searching for “from_str”.
Syntax
REPLACE (str,from_str, to_str);
Example
- SELECT REPLACE('www.mcnsolutions.net', 'w', 'W') AS STRING,
- REPLACE('V@TSA', '@', 'A') AS STRING,
- REPLACE('Stringg', 'gg', 'g') AS STRING;
In the RIGET(str,len) function, it returns the rightmost “len” characters from the String “str”. It returns the NULL if any argument is NULL.
Syntax
RIGHT (str, len);
Example
- SELECT RIGHT('Hello!India', 5) AS STRING,
- RIGHT('SunFlower', 6) AS STRING,
- RIGHT('Hello!Miss.', 5) AS STRING;
The RTRIM (str) function returns the String “str” with trailing space characters removed.
Syntax
RTRIM (str);
Example
- SELECT RTRIM(' Hello!Vatsa '),
- RTRIM('Hello! Vatsa '),
- RTRIM(' B S R ');
SPACE
The SPACE (N) function returns a String that consists of N space characters.
Syntax
SPACE (N);
Example
SUBSTRING(str,pos) and SUBSTRING(str FROM pos) return a substring from str String that is started at position pos. And the other two functions SUBSTRING(str,pos,len), SUBSTRING(str FROM pos FOR len) return a substring, that?s length is len characters, from String str and its started at position pos.
Syntax
SUBSTRING (str, pos),
SUBSTRING (str,FROM, pos),
SUBSTRING (str, pos,len),
SUBSTRING (str,FROM pos FOR len);
Example
- SELECT SUBSTRING('HelloIndia', 5),
- SUBSTRING('HelloIndia', FROM, 5),
- SUBSTRING('Hello!Vatsa...', 7, 5);
UPPER (str) function return the String “str”. And, in this string all the characters are changed in the uppercase.
Syntax
UPPER (str);
Example
- SELECT UPPER('helloIndia'),
- UPPER('vatsa'),
- UPPER('Hello! Vatsa...');
CONCLUSION
In this article, I have discussed the concept of MySQL String Functions with various examples.
I hope you enjoyed this article. Follow C# Corner to learn more new and amazing things about MySQL.
Thanks for reading this article!