mysql> CREATE TABLE tb1 (empid VARCHAR(10), nameVARCHAR(10), age INT);mysql> INSERT INTO tb1 VALUES('A101', 'ABC', 40);mysql> INSERT INTO tb1 VALUES('A102', 'DEF', 28);mysql> INSERT INTO tb1 VALUES('A103', 'GHI', 20);mysql> INSERT INTO tb1 VALUES('A104', 'JKL', 23);mysql> INSERT INTO tb1 VALUES('A105', 'MNO', 35);
String concatenation:
SELECT CONCAT(empid, 'Mr.', name) FROM tb1;
Slice from the right:
SELECT RIGHT(empid, 2) FROM tb1;
Slice from the left:
SELECT LEFT(empid, 2) FROM tb1;
Slice starting from the 2nd character of empid and get a substring of length 3:
SELECT SUBSTRING(empid, 2, 3) FROM tb1;
Repeat for a variable times (used for drawing graphs):
SELECT REPEAT('.', age) FROM tb1;
Print a string reversely:
SELECT REVERSE(name) FROM tb1;
Date and Time
Get current time with NOW():
mysql> CREATE TABLE t_now (a INT AUTO_INCREMENT PRIMARY KEY, b DATETIME);mysql> INSERT INTO t_now (b) VALUES(NOW());mysql> INSERT INTO t_now (b) VALUES(NOW());mysql> INSERT INTO t_now (b) VALUES(NOW());mysql> INSERT INTO t_now (b) VALUES(NOW());mysql> INSERT INTO t_now (b) VALUES(NOW());