Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display count of numeric and non numeric column value in a single row in mysql

Tags:

sql

mysql

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.

like image 401
Anurag Sharma Avatar asked Aug 20 '14 07:08

Anurag Sharma


People also ask

How do you find the values of non numeric columns in a table?

This is the way: SELECT * FROM TABLE_NAME WHERE NOT REGEXP_LIKE(COLUMN_NAME, '^-?[0-9.]+$ ');

How do I count values in a column in MySQL?

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.

How do I check if a column is numeric in MySQL?

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.


1 Answers

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 |
+-------+-------+
like image 163
Vaibhav Kumar Avatar answered Oct 05 '22 23:10

Vaibhav Kumar