I want to find all rows in which a column contains only digits.
The first idea was using LIKE [0-9]%, but in fact this matches any strings starting with a digit.
It seems to be a very simple task, but unfortunately I can't find solution
In Sql server use Not Like
where some_column NOT LIKE '%[^0-9]%'
Demo
declare #str varchar(50)='asdarew345'
select 1 where #str NOT LIKE '%[^0-9]%'
SQL FIDDLE DEMO
For Mysql use REGEX
SELECT *
FROM yourtable
WHERE string_column REGEXP '^[0-9]+$'
Related
I am trying to getting just the first two words on sql query, I am using the match: ^\w{2}- but with no success because nothing is coming to me, I need to get those values
BA, CE, DF, ES, GO, I don't know how can I do that, below some data example.
SC&Tipo=FM
SC&Tipo=Web
SC&Tipo=Comunitaria
RS&Tipo=Todas
RS&Tipo=AM
RS&Tipo=FM
RS&Tipo=Web
RS&Tipo=Comunitaria
BA-Salvador&Tipo=12horas
CE-Fortaleza&Tipo=12horas
CE-Interior&Tipo=12horas
DF-Brasilia&Tipo=12horas
ES-Interior&Tipo=12horas
ES-Vitoria&Tipo=12horas
GO-Goiania&Tipo=12horas
MG-ZonaDaMata/LestedeMinas&Tipo=12horas
MG-AltoParanaiba&Tipo=12horas
MG-BeloHorizonte&Tipo=12horas
MG-CentroOestedeMinas&Tipo=12horas
Query: SELECT * FROM tabel WHERE filter REGEXP '^\w{2}-'
EDIT SOLVED:
To solve the query should be:
SELECT SUBSTRING(column, 1, 2) AS column FROM table WHERE column REGEXP '^[[:alnum:]_]{2}-'
MySQL doesn't support the character class \w or \d. Instead of \w you have to use [[:alnum:]]. You can find all the supported character classes on the official MySQL documentation.
So you can use the following solution using REGEXP:
SELECT *
FROM table_name
WHERE filter REGEXP '^[[:alnum:]]{2}-'
You can use the following to get the result with regular expression too, using REGEXP_SUBSTR:
SELECT REGEXP_SUBSTR(filter, '^[[:alnum:]]{2}-')
FROM table_name
WHERE filter REGEXP '^[[:alnum:]]{2}-';
Or another solution using HAVING to filter the result:
SELECT REGEXP_SUBSTR(filter, '^[[:alnum:]]{2}-') AS colResult
FROM table_name
HAVING colResult IS NOT NULL;
To get the value before MySQL 8.0 you can use the following with LEFT:
SELECT LEFT(filter, 3)
FROM table_name
WHERE filter REGEXP '^[[:alnum:]]{2}-';
demo: https://www.db-fiddle.com/f/7mJEmCkEiYhCYK3PcEZTNE/0
Using SUBSTRING(<column>, 1, 2) should also work..
More or less like below
SELECT
<column>
, SUBSTRING(<column>, 1, 2)
FROM
<table>
WHERE
SUBSTRING(<column>, 1, 2) IN ('BA' [,<value>..])
Some things are BNF (Backus-Naur form) in the SQL code.
<..> means replace with what you need.
[, ..] means optional unlimited repeat the comma in there is part off SQL syntax
I want to to create a regex to find all columns that only have a single character ([A-Z]) as name, like N or M but not NM.
I've tried:
SELECT * FROM 'table' WHERE Name REGEXP '^[A-Z]'
But it's not displaying the expected result.
Try ^[A-Z]$.
You then state that this character is first and also last character of the value.
The regex functions in Oracle work only on one column. So, to search for just one character in a column, you would do the following:
select * from yourTable where REGEXP_LIKE (col1, '^[A-z]$');
Now, to search all the char/varchar columns on your table, you'll need to chain the regex expressions together, like so:
select * from yourTable where REGEXP_LIKE (col1, '^[A-z]$') or REGEXP_LIKE (col3, '^[A-z]$');
SQL solution:
where name in ('N','M')
just as the question can we do something to get the length and first 3 characters of the employee name of one column
Please do not mark as answered or duplicate
i have the test tomorrow Advance SQL so I am trying to solve some imp question..
Please answer the problem
thanks again
Hi Shanu, You can use LEN() or LENGTH()(in case of oracle sql) function to get the length of a column.
SELECT LEN(column_name) FROM table_name;
And you can use SUBSTRING or SUBSTR() function go get first three characters of a column.
SUBSTRING( string, start_position, length );
SELECT SUBSTRING( column_name, 1, 3 ) FROM table_name;
To get both together use concatenation operator,
SELECT LEN(column_name)||SUBSTRING( column_name, 1, 3 ) FROM table_name;
Hope you got what you need. Any issues, feel free to ask
We can use SUBSTRING or SUBSTR() function, go get first three characters of a column.
And then try this particular query:
SELECT SUBSTRING(ename,1,3)
FROM emp;
Select len(ename) as Column_Length, left(ename,3) first_three_char from employee; ---------need to code your query. Should not use test format, will be confusing
You can also use substring function instead of left. Query will look like
Select len(ename) as Column_Length,substring(ename,1,3) first_three_char from employee;
SELECT LEN(EMPLOYEE_NAME),LEFT(EMPLOYEE_NAME,3) FROM EMPLOYEE_TABLE;
I'm trying to select all rows that contain only alphanumeric characters in MySQL using:
SELECT * FROM table WHERE column REGEXP '[A-Za-z0-9]';
However, it's returning all rows, regardless of the fact that they contain non-alphanumeric characters.
Try this code:
SELECT * FROM table WHERE column REGEXP '^[A-Za-z0-9]+$'
This makes sure that all characters match.
Your statement matches any string that contains a letter or digit anywhere, even if it contains other non-alphanumeric characters. Try this:
SELECT * FROM table WHERE column REGEXP '^[A-Za-z0-9]+$';
^ and $ require the entire string to match rather than just any portion of it, and + looks for 1 or more alphanumberic characters.
You could also use a named character class if you prefer:
SELECT * FROM table WHERE column REGEXP '^[[:alnum:]]+$';
Try this:
REGEXP '^[a-z0-9]+$'
As regexp is not case sensitive except for binary fields.
There is also this:
select m from table where not regexp_like(m, '^[0-9]\d+$')
which selects the rows that contains characters from the column you want (which is m in the example but you can change).
Most of the combinations don't work properly in Oracle platforms but this does. Sharing for future reference.
Try this
select count(*) from table where cast(col as double) is null;
Change the REGEXP to Like
SELECT * FROM table_name WHERE column_name like '%[^a-zA-Z0-9]%'
this one works fine
I need to write a query using MYSQL REGEXP that will find me rows that have a certain column begin with 11 or 12 or etc. I know I can use LIKE or LEFT(####,2) but would like to use the REGEXP option. My data is stored as 110001, 122122, 130013a and etc.
EDIT 1:
To make it more clear, I would like to express
SELECT * FROM table WHERE column LIKE '11%' or column LIKE '12%' or column LIKE '30%'"
with REGEXP
Thanks!
To match rows that start with any two digits you can use:
SELECT *
FROM yourtable
WHERE yourcolumn REGEXP '^[0-9][0-9]';
If you only want to allow rows starting with 11, 12 or 30 then you could use this:
SELECT *
FROM yourtable
WHERE yourcolumn REGEXP '^(11|12|30)';
This will not be able to use an index on the column so it may be slower than using LIKE.