
- 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
Parse a string to get a number from a large string separated by underscore
Let us first create a table −
mysql> create table DemoTable1961 ( Title text ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1961 values('You_can_remove_the_string_part_only-10001-But_You_can_not_remove_the_numeric_parts'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1961;
This will produce the following output −
+------------------------------------------------------------------------------------+ | Title | +------------------------------------------------------------------------------------+ | You_can_remove_the_string_part_only-10001-But_You_can_not_remove_the_numeric_parts | +------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
Here is the query to implement CAST() and parse a string to get number from a string −
mysql> select cast(replace(replace('You_can_remove_the_string_part_only-10001-But_You_can_not_remove_the_numeric_parts','You_can_remove_the_string_part_only-',''), '-But_You_can_not_remove_the_numeric_parts','') as unsigned) as Output from DemoTable1961;
This will produce the following output −
+--------+ | Output | +--------+ | 10001 | +--------+ 1 row in set (0.00 sec)
- Related Articles
- How to parse a string from a JavaScript array?
- MySQL query to get a single value from position of comma-separated string?
- How to create a comma separated string from a list of string in C#?
- MySQL query to change a string by displaying only the part of string after underscore?
- How to match underscore in a MySQL String?
- Fetch substrings from a string with words separated by slash in MySQL?
- Convert a List of String to a comma separated String in Java
- Convert a Set of String to a comma separated String in Java
- Parse a string to a Boolean object in Java
- Java Program to Convert a List of String to Comma Separated String
- How to sum a comma separated string (string with numbers) in MySQL?
- Summing numbers present in a string separated by spaces using JavaScript
- How to parse a string into a nullable int in C#?
- How would you make a comma-separated string from a list in Python?
- How to parse a string to an int in C++?

Advertisements