MySQL String Functions
ASCII and ORD Functions
mysql> SELECT ASCII('A');
+------------+
| ASCII('A') |
+------------+
| 65 |
+------------+
1 row in set (0.00 sec)
mysql> SELECT ASCII('a');
+------------+
| ASCII('a') |
+------------+
| 97 |
+------------+
1 row in set (0.00 sec)
mysql> SELECT ORD('A');
+----------+
| ORD('A') |
+----------+
| 65 |
+----------+
1 row in set (0.00 sec)
mysql> SELECT ORD('a');
+----------+
| ORD('a') |
+----------+
| 97 |
+----------+
1 row in set (0.00 sec)
LENGHT and CHAR_LENGTH Functions
mysql> SELECT LENGTH("GoDarda") AS Length;
+--------+
| Length |
+--------+
| 12 |
+--------+
1 row in set (0.00 sec)
mysql> SELECT CHAR_LENGTH("GoDarda") AS Length;
+--------+
| Length |
+--------+
| 12 |
+--------+
1 row in set (0.00 sec)
CONCAT Function
mysql> SELECT CONCAT("Koding","Window") AS Concatenation;
+---------------+
| Concatenation |
+---------------+
| GoDarda |
+---------------+
1 row in set (0.00 sec)
mysql> SELECT CONCAT("Koding","Window"," Experiments") AS Concatenation;
+--------------------------+
| Concatenation |
+--------------------------+
| GoDarda Experiments |
+--------------------------+
1 row in set (0.00 sec)
LCASE and UCASE Functions
mysql> SELECT LOWER("KoDiNgWiNdOw") AS Lowercase;
+--------------+
| Lowercase |
+--------------+
| godarda |
+--------------+
1 row in set (0.12 sec)
mysql> SELECT UPPER("GoDarda") AS Uppercase;
+--------------+
| Uppercase |
+--------------+
| KODINGWINDOW |
+--------------+
1 row in set (0.00 sec)
mysql> SELECT LCASE("KoDiNgWiNdOw") AS Lowercase;
+--------------+
| Lowercase |
+--------------+
| godarda |
+--------------+
1 row in set (0.00 sec)
mysql> SELECT UCASE("GoDarda") AS Uppdercase;
+--------------+
| Uppdercase |
+--------------+
| KODINGWINDOW |
+--------------+
1 row in set (0.00 sec)
LEFT and RIGHT Functions
mysql> SELECT LEFT("GoDarda",6) AS Extraction;
+------------+
| Extraction |
+------------+
| Koding |
+------------+
1 row in set (0.00 sec)
mysql> SELECT RIGHT("GoDarda",6) AS Extraction;
+------------+
| Extraction |
+------------+
| Window |
+------------+
1 row in set (0.00 sec)
LOCATE and POSITION Functions
mysql> SELECT LOCATE("O","KODINGWINDOW") AS Locate;
+--------+
| Locate |
+--------+
| 2 |
+--------+
1 row in set (0.00 sec)
mysql> SELECT LOCATE("window","KODINGWINDOW") AS Locate;
+--------+
| Locate |
+--------+
| 7 |
+--------+
1 row in set (0.00 sec)
mysql> SELECT POSITION("O" IN "KODINGWINDOW") AS Position;
+----------+
| Position |
+----------+
| 2 |
+----------+
1 row in set (0.00 sec)
mysql> SELECT POSITION("window" IN "KODINGWINDOW") AS Position;
+----------+
| Position |
+----------+
| 7 |
+----------+
1 row in set (0.00 sec)
SUBSTR and MID Functions
mysql> SELECT SUBSTR("GoDarda",7,12) AS Substring;
+-----------+
| Substring |
+-----------+
| Window |
+-----------+
1 row in set (0.00 sec)
mysql> SELECT SUBSTRING("GoDarda",7,12) AS Substring;
+-----------+
| Substring |
+-----------+
| Window |
+-----------+
1 row in set (0.00 sec)
mysql> SELECT MID("GoDarda",7,12) AS Substring;
+-----------+
| Substring |
+-----------+
| Window |
+-----------+
1 row in set (0.00 sec)
LTRIM, RTRIM, and TRIM Functions
mysql> SELECT LTRIM(" GoDarda ") AS "Left Trim";
+-----------------+
| Left Trim |
+-----------------+
| GoDarda |
+-----------------+
1 row in set (0.00 sec)
mysql> SELECT RTRIM(" GoDarda ") AS "Right Trim";
+----------------+
| Right Trim |
+----------------+
| GoDarda |
+----------------+
1 row in set (0.00 sec)
mysql> SELECT TRIM(" GoDarda ") AS "Trim";
+--------------+
| Trim |
+--------------+
| GoDarda |
+--------------+
1 row in set (0.00 sec)
REPEAT, REPLACE, and REVERSE Functions
mysql> SELECT REPEAT(2,10) AS "Repeat";
+------------+
| Repeat |
+------------+
| 2222222222 |
+------------+
1 row in set (0.00 sec)
mysql> SELECT REPEAT("GD",5) AS "Repeat";
+------------+
| Repeat |
+------------+
| KWKWKWKWKW |
+------------+
1 row in set (0.00 sec)
mysql> SELECT REPLACE("GoDarda","Koding","Learning") AS "Replace";
+----------------+
| Replace |
+----------------+
| LearningWindow |
+----------------+
1 row in set (0.00 sec)
mysql> SELECT REVERSE("GoDarda") AS "Reverse";
+--------------+
| Reverse |
+--------------+
| wodniWgnidoK |
+--------------+
1 row in set (0.00 sec)
LPAD and RPAD Functions
mysql> SELECT LPAD("K",10,"W") AS "Left Padding";
+--------------+
| Left Padding |
+--------------+
| WWWWWWWWWK |
+--------------+
1 row in set (0.00 sec)
mysql> SELECT LPAD(152212,10,0) AS "Left Padding";
+--------------+
| Left Padding |
+--------------+
| 0000152212 |
+--------------+
1 row in set (0.00 sec)
mysql> SELECT RPAD("K",10,"W") AS "Right Padding";
+---------------+
| Right Padding |
+---------------+
| KWWWWWWWWW |
+---------------+
1 row in set (0.00 sec)
mysql> SELECT RPAD("K",10,"Z") AS "Right Padding";
+---------------+
| Right Padding |
+---------------+
| KZZZZZZZZZ |
+---------------+
1 row in set (0.00 sec)
SPACE Function
mysql> SELECT SPACE(50); +----------------------------------------------------+ | SPACE(50) | +----------------------------------------------------+ | | +----------------------------------------------------+ 1 row in set (0.00 sec)
FORMAT Function
mysql> SELECT FORMAT(15247892233,1); +-----------------------+ | FORMAT(15247892233,1) | +-----------------------+ | 15,247,892,233.0 | +-----------------------+ 1 row in set (0.00 sec) mysql> SELECT FORMAT(15247892233,2); +-----------------------+ | FORMAT(15247892233,2) | +-----------------------+ | 15,247,892,233.00 | +-----------------------+ 1 row in set (0.00 sec)
Comments and Reactions
What Next?
MySQL Data Definition