Tuesday, 2 August 2016

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

No comments:

Post a Comment