Tuesday, 2 August 2016

UPPER and LOWER functions



UPPER(string): It takes a string and convert all the characters to uppercase.
             
LOWER(string): It takes a string and convert all the characters to lowercase.

mysql> SELECT * FROM employee;
+----+--------------+------------+------------+
| id | firstName    | lastName   | salary     |
+----+--------------+------------+------------+
|  1 | Hari Krishna | Gurram     |   12345.67 |
|  2 | Rama Devi    | Gurram     | 1234578.67 |
|  3 | Lakshmana    | Rao        | 9876543.67 |
|  4 | Rama         | Krishna    | 1234587.67 |
|  5 | Sowmya       | asd        | 1238745.67 |
|  6 | Jyotsna      | PS         |   76543.67 |
|  7 | Gireesh      | Amara      |   87698.00 |
|  8 | Sravani      | Nidamanuri |  987654.00 |
|  9 | Saranya      | Amara      | 1987654.00 |
+----+--------------+------------+------------+
9 rows in set (0.00 sec)

mysql> SELECT UPPER(firstName) FROM employee;
+------------------+
| UPPER(firstName) |
+------------------+
| HARI KRISHNA     |
| RAMA DEVI        |
| LAKSHMANA        |
| RAMA             |
| SOWMYA           |
| JYOTSNA          |
| GIREESH          |
| SRAVANI          |
| SARANYA          |
+------------------+
9 rows in set (0.01 sec)

mysql> SELECT LOWER(firstName) FROM employee;
+------------------+
| LOWER(firstName) |
+------------------+
| hari krishna     |
| rama devi        |
| lakshmana        |
| rama             |
| sowmya           |
| jyotsna          |
| gireesh          |
| sravani          |
| saranya          |
+------------------+
9 rows in set (0.00 sec)



Previous                                                 Next                                                 Home

CONCAT: Concatenate strings



CONCAT(string1, stirng2…stringN)
CONCAT function takes more than one string as argument and concatenate them, return new string.

I am going to use following sample data.

CREATE TABLE employee(
  id int PRIMARY KEY,
  firstName VARCHAR(20),
  lastName VARCHAR(20),
  salary DECIMAL(10, 2)
);

INSERT INTO employee VALUES (1, "Hari Krishna", "Gurram", 12345.67);
INSERT INTO employee VALUES (2, "Rama Devi", "Gurram", 1234578.67);
INSERT INTO employee VALUES (3, "Lakshmana", "Rao", 9876543.67);
INSERT INTO employee VALUES (4, "Rama", "Krishna", 1234587.67);
INSERT INTO employee VALUES (5, "Sowmya", "asd", 1238745.67);
INSERT INTO employee VALUES (6, "Jyotsna", "PS", 76543.67);
INSERT INTO employee VALUES (7, "Gireesh", "Amara", 87698);
INSERT INTO employee VALUES (8, "Sravani", "Nidamanuri", 987654);
INSERT INTO employee VALUES (9, "Saranya", "Amara", 1987654);

mysql> SELECT * FROM employee;
+----+--------------+------------+------------+
| id | firstName    | lastName   | salary     |
+----+--------------+------------+------------+
|  1 | Hari Krishna | Gurram     |   12345.67 |
|  2 | Rama Devi    | Gurram     | 1234578.67 |
|  3 | Lakshmana    | Rao        | 9876543.67 |
|  4 | Rama         | Krishna    | 1234587.67 |
|  5 | Sowmya       | asd        | 1238745.67 |
|  6 | Jyotsna      | PS         |   76543.67 |
|  7 | Gireesh      | Amara      |   87698.00 |
|  8 | Sravani      | Nidamanuri |  987654.00 |
|  9 | Saranya      | Amara      | 1987654.00 |
+----+--------------+------------+------------+
9 rows in set (0.00 sec)

mysql> SELECT CONCAT(firstName, lastName) FROM employee;
+-----------------------------+
| CONCAT(firstName, lastName) |
+-----------------------------+
| Hari KrishnaGurram          |
| Rama DeviGurram             |
| LakshmanaRao                |
| RamaKrishna                 |
| Sowmyaasd                   |
| JyotsnaPS                   |
| GireeshAmara                |
| SravaniNidamanuri           |
| SaranyaAmara                |
+-----------------------------+
9 rows in set (0.00 sec)

mysql>
mysql> SELECT CONCAT(firstName, lastName) AS full_name FROM employee;
+--------------------+
| full_name          |
+--------------------+
| Hari KrishnaGurram |
| Rama DeviGurram    |
| LakshmanaRao       |
| RamaKrishna        |
| Sowmyaasd          |
| JyotsnaPS          |
| GireeshAmara       |
| SravaniNidamanuri  |
| SaranyaAmara       |
+--------------------+



Can I concat non-string data also?
Of course, you can.

mysql> DESC employee;
+-----------+---------------+------+-----+---------+-------+
| Field     | Type          | Null | Key | Default | Extra |
+-----------+---------------+------+-----+---------+-------+
| id        | int(11)       | NO   | PRI | NULL    |       |
| firstName | varchar(20)   | YES  |     | NULL    |       |
| lastName  | varchar(20)   | YES  |     | NULL    |       |
| salary    | decimal(10,2) | YES  |     | NULL    |       |
+-----------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql>
mysql> SELECT CONCAT(id, ". ", firstName, lastName) AS full_name FROM employee;
+-----------------------+
| full_name             |
+-----------------------+
| 1. Hari KrishnaGurram |
| 2. Rama DeviGurram    |
| 3. LakshmanaRao       |
| 4. RamaKrishna        |
| 5. Sowmyaasd          |
| 6. JyotsnaPS          |
| 7. GireeshAmara       |
| 8. SravaniNidamanuri  |
| 9. SaranyaAmara       |
+-----------------------+
9 rows in set (0.00 sec)









Previous                                                 Next                                                 Home

RAND: Generate random number


mysql> SELECT RAND();
+---------------------+
| RAND()              |
+---------------------+
| 0.45202005044745286 |
+---------------------+
1 row in set (0.00 sec)

mysql> SELECT RAND();
+--------------------+
| RAND()             |
+--------------------+
| 0.7262260548083355 |
+--------------------+
1 row in set (0.00 sec)

mysql> SELECT RAND();
+---------------------+
| RAND()              |
+---------------------+
| 0.27507015343296354 |
+---------------------+
1 row in set (0.00 sec)




Previous                                                 Next                                                 Home

TRUNCATE: Truncate decimal number



TRUNCATE(number, digits): Truncate the digits after decimal point.

mysql> SELECT TRUNCATE(10.2345, 0);
+----------------------+
| TRUNCATE(10.2345, 0) |
+----------------------+
|                   10 |
+----------------------+
1 row in set (0.00 sec)

mysql> SELECT TRUNCATE(10.2345, 1);
+----------------------+
| TRUNCATE(10.2345, 1) |
+----------------------+
|                 10.2 |
+----------------------+
1 row in set (0.00 sec)

mysql> SELECT TRUNCATE(10.2345, 2);
+----------------------+
| TRUNCATE(10.2345, 2) |
+----------------------+
|                10.23 |
+----------------------+
1 row in set (0.00 sec)

mysql> SELECT TRUNCATE(10.2345, 3);
+----------------------+
| TRUNCATE(10.2345, 3) |
+----------------------+
|               10.234 |
+----------------------+
1 row in set (0.00 sec)

mysql> SELECT TRUNCATE(10.2345, 4);
+----------------------+
| TRUNCATE(10.2345, 4) |
+----------------------+
|              10.2345 |
+----------------------+
1 row in set (0.00 sec)

mysql> SELECT TRUNCATE(10.2345, 5);
+----------------------+
| TRUNCATE(10.2345, 5) |
+----------------------+
|             10.23450 |
+----------------------+
1 row in set (0.00 sec)






Previous                                                 Next                                                 Home