Wednesday, 25 July 2018

MySQL String Functions

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.
LEFT (str,len): This will return left most character from string and if you pass NULL argument, returns NULL result.
RIGHT (str,len) : This will return right most character from string and if you pass NULL argument, returns NULL result.
INSTR (str,substr) : This function is used to find starting position of given substring.
LOCATE (substr,str) : This function work same as INSTR(str,substr)
POSITION (substr IN str) : This function work same as INSTR(str,substr)
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.
LENGTH(str): This function is used to find length of given string.
UPPER(str) / LOWER(str) : This both function is used to change string from lower to upper or upper to lowar case.
REPEAT (str,count): This function is used to repeat string by given count.
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.
REVERSE(str) : This function is used to reverse each word of string.
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.
SUBSTRING (str,pos) : This function is used to return sub part of string by giving position.
SUBSTRING (str,pos,len): This function is used to return sub part of string by giving position and length of return string.
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.
Above all are basic and important String manipulation function in MySQL.

0 comments:

Post a Comment