0% found this document useful (0 votes)
10 views8 pages

String Functions Ip

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

String Functions Ip

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

FUNCTIONS IN MYSQL

The syntax to use a function :


SELECT FUNCTION_NAME(COLUMN_NAME) FROM TABLE_NAME;

Functions in MySql are classified into two types:


a) Single row function
b) Multiple row function

a) Single row function:


 These functions operate on a single value to return a single value.
 They can accept one or more arguments but return only one result per row.
 They can be used with SELECT,WHERE and ORDER BY clause.
 They are further categorised into:
i) String functions
ii) Numeric functions
iii) Date and Time functions

i) String functions:

1. ASCII(str):
 Returns numeric value of left-most character.
 Returns 0 if str is the empty string.
 Returns NULL if str is NULL.
 ASCII() works for characters with numeric values from 0 to 255.

mysql> SELECT ASCII('2');


+---------------------------------------------------------+
| ASCII('2') |
+---------------------------------------------------------+
| 50 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT ASCII('dx');


+---------------------------------------------------------+
| ASCII('dx') |
+---------------------------------------------------------+
| 100 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

2. CHAR(N,... [USING charset_name])


 CHAR() interprets each argument N as an integer and returns a string
consisting of the characters given by the code values of those integers.
 NULL values are skipped.

mysql> SELECT CHAR(77,121,83,81,'76');


+---------------------------------------------------------+
| CHAR(77,121,83,81,'76') |
+---------------------------------------------------------+
| MySQL |
+---------------------------------------------------------+
1 row in set (0.00 sec)

3. LENGTH(str)

 Returns the length of the string str, measured in bytes.


mysql> SELECT LENGTH('text');
+---------------------------------------------------------+
| LENGTH('text') |
+---------------------------------------------------------+
| 4 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

4. CONCAT(str1,str2,...)

 Returns the string that results from concatenating the arguments. May have
one or more arguments.
mysql> SELECT CONCAT('My', 'S', 'QL');
+---------------------------------------------------------+
| CONCAT('My', 'S', 'QL') |
+---------------------------------------------------------+
| MySQL |
+---------------------------------------------------------+
1 row in set (0.00 sec)

5. INSERT(str,pos,len,newstr)

 Returns the string str, with the substring beginning at position pos and len
characters long replaced by the string newstr.
 Returns the original string if pos is not within the length of the string.
 Replaces the rest of the string from position pos if len is not within the length
of the rest of the string.
 Returns NULL if any argument is NULL.
mysql> SELECT INSERT('Quadratic', 3, 10, 'What');
+---------------------------------------------------------+
| INSERT('Quadratic', 3, 10, 'What') |
+---------------------------------------------------------+
| QuWhat |
+---------------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT INSERT('Quadratic', 3, 4, 'What');
+---------------------------------------------------------+
| INSERT('Quadratic', 3, 4, 'What') |
+---------------------------------------------------------+
| QuWhattic |
+---------------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT INSERT('Quadratic', 3, 2, 'What');
+---------------------------------------------------------+
| INSERT('Quadratic', 3, 2, 'What') |
+---------------------------------------------------------+
| QuWhatratic |
+---------------------------------------------------------+

6. INSTR(str,substr)

 Returns the position of the first occurrence of substring substr in string str.

mysql> SELECT INSTR('foobarbar', 'bar');


+---------------------------------------------------------+
| INSTR('foobarbar', 'bar') |
+---------------------------------------------------------+
| 4 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

7. LEFT(str,len)
 Returns the leftmost len characters from the string str, or NULL if any
argument is NULL.
mysql> SELECT LEFT('foobarbar', 5);
+---------------------------------------------------------+
| LEFT('foobarbar', 5) |
+---------------------------------------------------------+
| fooba |
+---------------------------------------------------------+
1 row in set (0.00 sec)

8. LOWER(str)/ LCASE(str)

 Returns the string str with all characters changed to lowercase according
to the current character set mapping.
mysql> SELECT LOWER('QUADRATICALLY');
+---------------------------------------------------------+
| LOWER('QUADRATICALLY') |
+---------------------------------------------------------+
| quadratically |
+---------------------------------------------------------+
1 row in set (0.00 sec)

9. LPAD(str,len,padstr)

 Returns the string str, left-padded with the string padstr to a length of len
characters.
 If str is longer than len, the return value is shortened to len characters.
mysql> SELECT LPAD('python',4,'??');
+---------------------------------------------------------+
| LPAD('python',4,'??') |
+---------------------------------------------------------+
| pyth |
+---------------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT LPAD('hi',4,'??');
+---------------------------------------------------------+
| LPAD('hi',4,'??') |
+---------------------------------------------------------+
| ??hi |
+---------------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT LPAD('h',4,'??');
+---------------------------------------------------------+
| LPAD('h',4,'??') |
+---------------------------------------------------------+
| ???h |
+---------------------------------------------------------+
1 row in set (0.00 sec)

10. LTRIM(str)
 Returns the string str with leading space characters removed.
mysql> SELECT LTRIM(' barbar');
+---------------------------------------------------------+
| LTRIM(' barbar') |
+---------------------------------------------------------+
| barbar |
+---------------------------------------------------------+
1 row in set (0.00 sec)

11. REPEAT(str,count)
 Returns a string consisting of the string str repeated count times. If count
is less than 1, returns an empty string. Returns NULL if str or count are
NULL.
mysql> SELECT REPEAT('MySQL', 3);
+---------------------------------------------------------+
| REPEAT('MySQL', 3) |
+---------------------------------------------------------+
| MySQLMySQLMySQL |
+---------------------------------------------------------+
1 row in set (0.00 sec)

12. REPLACE(str,from_str,to_str)
 Returns the string str with all occurrences of the string from_str replaced
by the string to_str. REPLACE() performs a case-sensitive match when
searching for from_str.
mysql> SELECT REPLACE('www.mysql.com', 'w', 'Ww');
+---------------------------------------------------------+
| REPLACE('www.mysql.com', 'w', 'Ww') |
+---------------------------------------------------------+
| WwWwWw.mysql.com |
+---------------------------------------------------------+
1 row in set (0.00 sec)

13. REVERSE(str)
 Returns the string str with the order of the characters reversed.
mysql> SELECT REVERSE('abcd');
+---------------------------------------------------------+
| REVERSE('abcd') |
+---------------------------------------------------------+
| dcba |
+---------------------------------------------------------+
1 row in set (0.00 sec)

14. RIGHT(str,len)
 Returns the rightmost len characters from the string str, or NULL if any
argument is NULL.
mysql> SELECT RIGHT('foobarbar', 4);
+---------------------------------------------------------+
| RIGHT('foobarbar', 4) |
+---------------------------------------------------------+
| rbar |
+---------------------------------------------------------+
1 row in set (0.00 sec)

15. RPAD(str,len,padstr)
 Returns the string str, right-padded with the string padstr to a length of
len characters. If str is longer than len, the return value is shortened to len
characters.
mysql> SELECT RPAD('hi',5,'?');
+---------------------------------------------------------+
| RPAD('hi',5,'?') |
+---------------------------------------------------------+
| hi??? |
+---------------------------------------------------------+
1 row in set (0.00 sec)

16. RTRIM(str)
 Returns the string str with trailing space characters removed.
mysql> SELECT RTRIM('barbar ');
+---------------------------------------------------------+
| RTRIM('barbar ') |
+---------------------------------------------------------+
| barbar |
+---------------------------------------------------------+
1 row in set (0.00 sec)

17. STRCMP(str1, str2)


 Compares two strings and returns 0 if both strings are equal, it returns -1
if the first argument is smaller than the second according to the current
sort order otherwise it returns 1.
mysql> SELECT STRCMP('MOHD', 'MOHD');
+---------------------------------------------------------+
| STRCMP('MOHD', 'MOHD') |
+---------------------------------------------------------+
| 0 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
Another example is −

mysql> SELECT STRCMP('AMOHD', 'MOHD');


+---------------------------------------------------------+
| STRCMP('AMOHD', 'MOHD') |
+---------------------------------------------------------+
| -1
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
Let's see one more example −

mysql> SELECT STRCMP('MOHD', 'AMOHD');


+---------------------------------------------------------+
| STRCMP('MOHD', 'AMOHD') |
+---------------------------------------------------------+
| 1
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
18. SUBSTRING(str,pos)/SUBSTRING(str,pos,len)/
 The forms without a len argument return a substring from string str
starting at position pos.
 The forms with a len argument return a substring len characters long from
string str, starting at position pos.
 It is also possible to use a negative value for pos. In this case, the
beginning of the substring is pos characters from the end of the string,
rather than the beginning.
 A negative value may be used for pos in any of the forms of this function.

mysql> SELECT SUBSTRING('Quadratically',5);


+---------------------------------------------------------+
| SUBSTRING('Quadratically',5) |
+---------------------------------------------------------+
| ratically |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT SUBSTRING('foobarbar' FROM 4);


+---------------------------------------------------------+
| SUBSTRING('foobarbar' FROM 4) |
+---------------------------------------------------------+
| barbar |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT SUBSTRING('Quadratically',5,6);


+---------------------------------------------------------+
| SUBSTRING('Quadratically',5,6) |
+---------------------------------------------------------+
| ratica |
+---------------------------------------------------------+
1 row in set (0.00 sec)

19. TRIM( str)


 Returns the string str with all leading and trailing spaces removed
mysql> SELECT TRIM(' bar ');
+---------------------------------------------------------+
| TRIM(' bar ') |
+---------------------------------------------------------+
| bar |
+---------------------------------------------------------+
1 row in set (0.00 sec)

20. UPPER(str)/ UCASE(str)

 Returns the string str with all characters changed to uppercase according to the
current character set mapping.
mysql> SELECT UPPER('python');
+---------------------------------------------------------+
| UPPER('python') |
+---------------------------------------------------------+
| PYTHON |
+---------------------------------------------------------+
1 row in set (0.00 sec)

You might also like