To combine strings rather than pull them apart, use the CONCAT( ) function. : CONCAT « String « SQL / MySQL






To combine strings rather than pull them apart, use the CONCAT( ) function.

       
mysql>
mysql> SELECT CONCAT('Hello, ',USER( ),', welcome to MySQL!') AS greeting;
+------------------------------------------+
| greeting                                 |
+------------------------------------------+
| Hello, root@localhost, welcome to MySQL! |
+------------------------------------------+
1 row in set (0.00 sec)

mysql>
mysql> CREATE TABLE mytable
    -> (
    ->  name    VARCHAR(20)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO mytable (name)
    ->  VALUES
    ->          ('copper'),
    ->          ('gold'),
    ->          ('iron'),
    ->          ('lead'),
    ->          ('mercury'),
    ->          ('platinum'),
    ->          ('silver'),
    ->          ('tin')
    -> ;
Query OK, 8 rows affected (0.00 sec)
Records: 8  Duplicates: 0  Warnings: 0

mysql>
mysql> SELECT * FROM mytable;
+----------+
| name     |
+----------+
| copper   |
| gold     |
| iron     |
| lead     |
| mercury  |
| platinum |
| silver   |
| tin      |
+----------+
8 rows in set (0.00 sec)

mysql>
mysql> SELECT CONCAT(name,' ends in "d":
    '> ',IF(RIGHT(name,1)='d','YES','NO'))
    -> AS 'ends in "d"?'
    -> FROM mytable;
+--------------------------+
| ends in "d"?             |
+--------------------------+
| copper ends in "d":
NO   |
| gold ends in "d":
YES    |
| iron ends in "d":
NO     |
| lead ends in "d":
YES    |
| mercury ends in "d":
NO  |
| platinum ends in "d":
NO |
| silver ends in "d":
NO   |
| tin ends in "d":
NO      |
+--------------------------+
8 rows in set (0.00 sec)

mysql>
mysql> drop table mytable;
Query OK, 0 rows affected (0.00 sec)

   
    
    
    
    
    
    
  








Related examples in the same category

1.CONCAT(, [{, }...])
2.Use the CONCAT() and LEFT() functions to create registration codes for the employees.
3.Manipulate retrieved data from the Employees table.
4.Combining a Date and a Time into a Date-and-Time Value
5.Canonizing Not-Quite-ISO Date Strings
6.Add the setting NO_ZERO_IN_DATE to the SQL_MODE system variable.
7.Concatenating Character Strings
8.Concatenation can be useful for modifying column values "in place."
9.Non-ISO results from the CONCAT( ) operation can be converted into ISO format three different ways as follows:
10.Generate Unique Sequential Numbers
11.Use Concat to create Initial for the name