
- 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
How to use comparison operator for numeric string in MySQL?
To use comparison operator for numeric string, use the substring() method. Let us first create a table −
mysql> create table DemoTable1881 ( UserId int, UserEducationGap varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1881 values(101,'5-9'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1881 values(102,'2-4'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1881 values(103,'4-8'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1881 values(104,'7-12'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1881;
This will produce the following output −
+--------+------------------+ | UserId | UserEducationGap | +--------+------------------+ | 101 | 5-9 | | 102 | 2-4 | | 103 | 4-8 | | 104 | 7-12 | +--------+------------------+ 4 rows in set (0.00 sec)
Here is the query to use comparison operator for numeric string in MySQL
mysql> select * from DemoTable1881 where cast(substring(UserEducationGap, 1, instr(UserEducationGap, '-')-1) as signed) >= 4;
This will produce the following output −
+--------+------------------+ | UserId | UserEducationGap | +--------+------------------+ | 101 | 5-9 | | 103 | 4-8 | | 104 | 7-12 | +--------+------------------+ 3 rows in set (0.00 sec)
- Related Articles
- How does comparison operator work with date values in MySQL?
- How to make SQL case sensitive string comparison in MySQL?
- Perform element-wise comparison of two string arrays using a comparison operator in Numpy
- How LIKE operator works with comparison operators for matching specific kinds of patterns of a string?
- What is MySQL NULL-safe equal operator and how it is different from comparison operator?
- How MySQL can perform case-sensitive string comparison?
- What is the use of MySQL BINARY keyword while performing string comparison?
- In MySQL, how does the precedence of ! operator in comparison with NOT operator depends upon HIGH_NOT_PRECEDENCE SQL mode?
- How to use MySQL FROM_UNIXTIME() function to return datetime value in numeric format?
- Why we cannot use comparison operator(=) for getting the rows with NULL from a table?
- How to perform string comparison in TypeScript?
- Object comparison Complexity in JavaScript using comparison operator or JSON.stringlify()?
- How to validate a string for a numeric representation using TryParse in C#
- Comparison between "&&" and "AND" operator in PHP.
- How can we use MySQL UNION operator on datasets?

Advertisements