I have a table test with a column names which have both integer as well as non integer values
+------------+
| names |
+------------+
| 123 |
| 123abc |
| 89 |
| dkkjdk |
| dkdn |
+------------+
I would like to display the count of integer and non integer values in a single row like this
integer_count non_integer_count
2 3
I tried to print integer values using this query
select cast(names as signed) as int_val from test;
But I got this result
+---------+
| int_val |
+---------+
| 123 |
| 123 |
| 89 |
| 0 |
| 0 |
+---------+
The name field is a varchar(20) field.
This is the way: SELECT * FROM TABLE_NAME WHERE NOT REGEXP_LIKE(COLUMN_NAME, '^-?[0-9.]+$ ');
Here is the query to count the number of times value (marks) appears in a column. The query is as follows. mysql> select Marks,count(*) as Total from CountSameValue group by Marks; The following is the output.
Syntax to check if the value is an integer. select yourColumnName from yourTableName where yourColumnName REGEXP '^-?[0-9]+$'; The query wherein we have used regular expression. This will output only the integer value.
Here is the solution
SELECT COUNT(CASE WHEN not names REGEXP '^[0-9]+$' THEN 1 END) AS ALPHA, COUNT(CASE WHEN names REGEXP '^[0-9]+$' THEN 1 END) AS DIGIT FROM test;
Output
+-------+-------+
| ALPHA | DIGIT |
+-------+-------+
| 3 | 2 |
+-------+-------+
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With