How can I SELECT rows that are not number - mysql

How can I select rows in MySQL that are not numbers?
SELECT *
FROM `TABLE`
WHERE `Column_B` <> Number
I know, this is a stupid question, but I haven't found a solution for this. Column_B is varchar(3).

Try this sqlfiddle..
http://sqlfiddle.com/#!2/17f28/1
SELECT * FROM Table1 WHERE col NOT REGEXP '^[0-9]+$'
If you want to match real numbers (floats) rather than integers, you need to handle the case above, along with cases where your pattern is between 0 and 1 (i.e. 0.25), as well as case where your pattern has a decimal part that is 0. (i.e. 2.0). And while we're at it, we'll add support for leading zeros on integers (i.e. 005):
^0*[1-9][0-9]*(\.[0-9]+)?|0+\.[0-9]*[1-9][0-9]*$
sql looks like SELECT * FROM Table1 WHERE col NOT REGEXP '^0*[1-9][0-9]*(\.[0-9]+)?|0+\.[0-9]*[1-9][0-9]*$'

This is one way of doing that, using mysql regexp operator (fiddle):
SELECT * FROM `TABLE`
WHERE `Column_B` NOT REGEXP '^-?[0-9]*\\.?[0-9]+([Ee][+-][0-9]+)?$';
Supports signed/unsigned integers, decimals, and scientific notation.
Note: it is recommended to avoid using sql keywords for entity names, even if you quote them with backticks.

this also will work for you
SELECT col FROM Table1
WHERE col NOT IN ( SELECT col FROM Table1
where CAST(col as DECIMAL(10,5)) !=0
or Col = '0' )
DEMO

Related

How to SQL select excluding strings with N duplicate characters in a row

How to make a select from a table excluding rows with N duplicate characters in a row in a certain column? Let's say N=5
'0000011114BR13471' // Exclude
'554XXXXXXXXXXXXXX' // Exclude
'000111114BR134716' // Exclude
'000011114BR134716' // Include
'11880000000000000' // Exclude
'12345678901200000' // Exclude
'12345678901200001' // Include
I tried many combinations but none of them worked. For example:
SELECT * FROM mytable WHERE not (mycolumn regexp '(.)\1{5,}');
Thank you!
You can use the LIKE to match regular Expression and EXCEPT clause to exclude unwanted Results. A query like this might work
In SQL SERVER
Select * from myTable
EXCEPT
Select * from myTable WHERE ColumnName like '(.)\1{4,}'
In MySQl
Select * from myTable
where ColumnName Not In(
Select ColumnName from myTable WHERE ColumnName RLIKE '(.)\1{4,}')
Here N=5. the 4 in regular expration represents 5 duplicates.
I don't think MySQL supports back-references in regular expressions -- which is a shame for your problem. One method is brute force:
select t.*
from t
where col not regexp '0{5}|1{5}|2{5}|3{5}|4{5}|5{5}|6{5}|7{5}|8{5}|9{5}|X{5}';
Another method is a recursive CTE, which breaks up the string into individual characters and then uses window functions to determine if there are 5 in a row:
with recursive cte as (
select col,left(col, 1) as chr, substr(col, 2) as rest, 1 as lev
from t
union all
select col, left(rest, 1), substr(rest, 2), lev + 1
from cte
where rest <> ''
)
select col
from (select cte.*,
lead(lev, 4) over (partition by col, chr order by lev) as chr_4
from cte
) x
group by col
having max(chr_4 = lev + 4) = 0
Here is a db<>fiddle.
In MySQL 8.0:
mycolumn regexp '(.)\\1{4}'
Notes:
Two backslashes are needed.
Since there is 1 selected (.), you need to check for only 4 more, not 5.
The , (meaning "or more") is unnecessary.

How to select strings which don't contain letters in SQL?

I have a column STR which may contain any strings. I'm using MySql. How to find strings which don't contain letters in SQL without using Regular Expressions? As I understand RegExp in SQL is [^...].
So how to select the strings without using [^...]?
Regexp is the most sensible way of doing this. An alternative without...
SELECT STR
FROM YourTable
WHERE NOT EXISTS (SELECT *
FROM (SELECT 'A' AS C
UNION ALL
SELECT 'B'
UNION ALL
SELECT 'C'
/* Todo. Add remaining letters */
) Chars
WHERE INSTR(STR, C) > 0)
I am not sure which RDBMS you're using. But, if you do not want to use regular expression, you can loop through every character in the string and check the ASCII code. If they are only falling in the range 48 to 57, they are only numbers.
Note : This may be very costly operation

How to select everything that does not start with s number in MySQL

I am trying to find all values that do not start with a number
I have tried this query, but not sure if the REGEXP is correct. This seem to be returning any value the does not contains a number
SELECT * FROM table where address NOT REGEXP '[0-9]'
I think this fixed the issue
SELECT * FROM table where address NOT REGEXP '^[0-9]'
In MySQL if you try to convert a string to a number then it will start from the beginning taking the digits and convert as much as possible.
If a string does not start with a number then the result is 0.
select * from your_table
where address * 1 = 0
SQLFiddle demo
You can use explicit conversion with cast(str_column as unsigned) or implicit conversion by using a mathematical operator like * 1

SUBSTR() in MYSQl

How to remove characters from end from any column in MYSQl using SUBSTR ?
For example:
Suppose column value is 1221213.2, so I want to achieve 1221213.
SELECT SUBSTR(colname,1,-2) FROM tablename.
It's this query (assuming your 'remove' is update data in DB):
UPDATE tablename SET colname=SUBSTR(colname, 1, CHAR_LENGTH(colname)-1)
-this will remove 1 symbol from end of string. (so, SUBSTR(colname, 1, CHAR_LENGTH(colname)-2) for 2 sumbols - you've updated)
(upd. there are suggestions for LEFT() function, it's better. I will not repeat that code, however).
It work for firebirb:
select substr(p.name, 1, strlen(rtrim(p.name))-2) from products p
I think for MySQL strlen need replaced with CHAR_LENGTH() function.
You can also use LEFT function.
select LEFT(colname, LENGTH(colname)-2);
LEFT(str, len) selects number of characters from left of the string specified.
SELECT LEFT(colname, CHARACTER_LENGTH(colname)-2);
Prefer CHARACTER_LENGTH() over LENGTH() to avoid surprises.
Now it looks like you are trying to truncate the decimal digits from a decimal number. This is what you might be looking for instead:
SELECT TRUNCATE(colname, 0);
And to update your table:
UPDATE tablename SET colname = TRUNCATE(colname, 0); -- or LEFT(...), or whatever approach you choose
The previous answers all assume that you always want to remove the last 2 characters. But what if there's more (or less) significant digits after the period?
UPDATE your_table
SET colname = SubStr(colname, 0, Locate('.', colname))
WHERE Locate('.', colname) > 0
UPDATE
Even better method:
UPDATE your_table
SET colname = SubString_Index(colname , '.', 1)
WHERE Locate('.', colname) > 0

How do you select from mysql where last character in a string = x?

I'd like to select rows from the database where the last character in the mov_id column equals to 1 and 2.
How would the query look like?
SELECT * FROM `myTable` WHERE `mov_id` LIKE '%1' OR `mov_id` LIKE '%2'
the % character is a wildcard which matches anything (like * in many other places)
If mov_id is a numeric value (TINYINT, INT, etc...) then you should use a numeric operator. For instance, use the modulo operator to keep the last digit
SELECT * FROM mytable
WHERE (mov_id MOD 10) IN (1, 2)
If mov_id is a string, you can use LIKE or SUBSTRING(). SUBSTRING() will be slightly faster.
SELECT * FROM mytable
WHERE SUBSTRING(mov_id, -1) IN ('1', '2')
If your table is big or that query is frequently run, you should definitely consider adding a column to your table, in which you would store mov_id's last digit/character, and index that column.
Try this way too:
SELECT field1
FROM table
WHERE RIGHT(field1, 1) = 'x'
it displays the fields that has last a value of x.
SELECT *
FROM Table
WHERE RIGHT(Column_name, 1) IN ('x')
if you want to match two character just replace 1 by 2.
In general:
RIGHT(COLUMN_NAME, NO_OF_CHARACTER_YOU WANT_TO_MATCH_FROM_LAST)
And if you want to match the starting char just use LEFT instead of RIGHT
You could also do something like:
select
your, fields, go, here
from table
where
substring(mov_id, (char_length(move_id) - 1)) = x
SELECT * FROM table WHERE mov_id REGEXP '1$' OR mov_id REGEXP '2$'