
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Remove all except the first character of a string in MySQL?
For this, use the LEFT() method, which returns a specified number of characters from the left of the string.
Let us first create a table −
mysql> create table DemoTable -> ( -> FirstName varchar(100) -> ); Query OK, 0 rows affected (0.96 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+-----------+ | FirstName | +-----------+ | Sam | | Mike | | David | +-----------+ 3 rows in set (0.00 sec)
Following is the query to remove all except the first character of a string −
mysql> update DemoTable set FirstName=left(FirstName,1); Query OK, 3 rows affected (0.13 sec) Rows matched: 3 Changed: 3 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
Output
This will produce the following output −
+-----------+ | FirstName | +-----------+ | S | | M | | D | +-----------+ 3 rows in set (0.00 sec)
- Related Articles
- Select all except the first character in a string in MySQL?
- How to remove the first character of string in PHP?
- C++ Program to Remove all Characters in a String Except Alphabets
- How can I remove all elements except the first one using jQuery?
- How to remove the first and last character in a string in R?
- Java program to remove all numbers in a string except "1" and "2"?
- Remove first two characters of all fields in MySQL?
- Remove all characters of first string from second JavaScript
- How to cut only the first character in MySQL string?
- How to remove all instances of a specific character from a column in MySQL?
- Python program to replace all Characters of a List except the given character
- How to remove all text from a string before a particular character in R?
- How to remove all common character from string array elements in android?
- How to find the first character of a string in C#?
- Finding the first non-repeating character of a string in JavaScript

Advertisements