Tutorialspoint

How to set a string with hyphen and numbers in MySQL varchar?


1 Answer
Sharon Christine

To set a string with hyphen and numbers, you need to use single quotes. For example, 'Customer-1234-899', 'Customer-9383-901', etc.

Let us first create a table −

mysql> create table DemoTable
   -> (
   -> CustomerId varchar(100)
   -> );
Query OK, 0 rows affected (0.70 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Customer-1234-899');
Query OK, 1 row affected (0.19 sec)

mysql> insert into DemoTable values('Customer-8373-900');
Query OK, 1 row affected (0.17 sec)

mysql> insert into DemoTable values('Customer-9383-901');
Query OK, 1 row affected (0.20 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable

This will produce the following output −

+-------------------+
| CustomerId        |
+-------------------+
| Customer-1234-899 |
| Customer-8373-900 |
| Customer-9383-901 |
+-------------------+
3 rows in set (0.00 sec)

Here is the query to display how we can select a single value −

mysql> select *from DemoTable where (CustomerId='Customer-8373-900');

This will produce the following output −

+-------------------+
| CustomerId        |
+-------------------+
| Customer-8373-900 |
+-------------------+
1 row in set (0.00 sec)

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.