I've been trying to write this query, I need to select the rows where a column has only letters (a-z) and a full stop.
I tried this but it's not working:
SELECT * FROM table WHERE (c1 REGEXP '[^a-zA-Z\.]') = 0
This one would usually work in PHP.
Try:
SELECT * FROM table WHERE c1 REGEXP '^[a-zA-Z.]+$'
The anchor ^ and $ ensure that you are matching the entire string and not part of it. Next the character class [a-zA-Z.] matches a single upper/lower case letter or a period. The + is the quantifier for one or more repetitions of the previous sub-regex, so in this case it allows us to match one or more of either a period or a upper/lower case letter.
More info on regex usage in MySQL
Related
I am trying to pull a product code from a long set of string formatted like a URL address. The pattern is always 3 letters followed by 3 or 4 numbers (ex. ???### or ???####). I have tried using REGEXP and LIKE syntax, but my results are off for both/I am not sure which operators to use.
The first select statement is close to trimming the URL to show just the code, but oftentimes will show a random string of numbers it may find in the URL string.
The second select statement is more rudimentary, but I am unsure which operators to use.
Which would be the quickest solution?
SELECT columnName, SUBSTR(columnName, LOCATE(columnName REGEXP "[^=\-][a-zA-Z]{3}[\d]{3,4}", columnName), LENGTH(columnName) - LOCATE(columnName REGEXP "[^=\-][a-zA-Z]{3}[\d]{3,4}", REVERSE(columnName))) AS extractedData FROM tableName
SELECT columnName FROM tableName WHERE columnName LIKE '%___###%' OR columnName LIKE '%___####%'
-- Will take a substring of this result as well
Example Data:
randomwebsite.com/3982356923abcd1ab?random_code=12480712_ABC_DEF_ANOTHER_CODE-xyz123&hello_world=us&etc_etc
In this case, the desired string is "xyz123" and the location of said pattern is variable based on each entry.
EDIT
SELECT column, LOCATE(column REGEXP "([a-zA-Z]{3}[0-9]{3,4}$)", column), SUBSTR(column, LOCATE(column REGEXP "([a-zA-Z]{3}[0-9]{3,4}$)", column), LENGTH(column) - LOCATE(column REGEXP "^.*[a-zA-Z]{3}[0-9]{3,4}", REVERSE(column))) AS extractData From mainTable
This expression is still not grabbing the right data, but I feel like it may get me closer.
I suggest using
REGEXP_SUBSTR(column, '(?<=[&?]random_code=[^&#]{0,256}-)[a-zA-Z]{3}[0-9]{3,4}(?![^&#])')
Details:
(?<=[&?]random_code=[^&#]{0,256}-) - immediately on the left, there must be & or &, random_code=, and then zero to 256 chars other than & and # followed with a - char
[a-zA-Z]{3} - three ASCII letters
[0-9]{3,4} - three to four ASCII digits
(?![^&#]) - that are followed either with &, # or end of string.
See the online demo:
WITH cte AS ( SELECT 'randomwebsite.com/3982356923abcd1ab?random_code=12480712_ABC_DEF_ANOTHER_CODE-xyz123&hello_world=us&etc_etc' val
UNION ALL
SELECT 'randomwebsite.com/3982356923abcd1ab?random_code=12480712_ABC_DEF_ANOTHER_CODE-xyz4567&hello_world=us&etc_etc'
UNION ALL
SELECT 'randomwebsite.com/3982356923abcd1ab?random_code=12480712_ABC_DEF_ANOTHER_CODE-xyz89&hello_world=us&etc_etc'
UNION ALL
SELECT 'randomwebsite.com/3982356923abcd1ab?random_code=12480712_ABC_DEF_ANOTHER_CODE-xyz00000&hello_world=us&etc_etc'
UNION ALL
SELECT 'randomwebsite.com/3982356923abcd1ab?random_code=12480712_ABC_DEF_ANOTHER_CODE-aaaaa11111&hello_world=us&etc_etc')
SELECT REGEXP_SUBSTR(val,'(?<=[&?]random_code=[^&#]{0,256}-)[a-zA-Z]{3}[0-9]{3,4}(?![^&#])') output
FROM cte
Output:
I'd make use of capture groups:
(?<=[=\-\\])([a-zA-Z]{3}[\d]{3,4})(?=[&])
I assume with [^=\-] you wanted to capture string with "-","\" or "=" in front but not include those chars in the result. To do that use "positive lookbehind" (?<=.
I also added a lookahead (?= for "&".
If you'd like to fidget more with regex I recommend RegExr
I suck at REGEX, but I need to pull all the records from a table column that stats with AST, and the rest only contains numbers after. I am assuming this can be done with just REGEX and not LIKE but I'm not sure.
For instance AST000001
and not AST99XXH011
SELECT * FROM table WHERE column LIKE 'AST%' AND column REGEXP '[0-9]$'
You can use REGEXP/RLIKE on the whole column value (using start-of-string (^) and end-of-string ($) anchors to ensure you match the entire column):
SELECT *
FROM `table`
WHERE `column` REGEXP '^AST[0-9]+$'
Demo on dbfiddle
I would like to query a database which is MySQL 5.
Let's say database name is db and the table name is table and the column name is column
and that column is a text
containing the following for example:
aksksksksjsjk&ct=100&lor=10
aksksksksjsjk&ct=1001001001001001&lor=10
So i would like to query that table and grep only where ct start with number 1 and it's 16 numbers.
I tried with SELECT column FROM db.table WHERE column LIKE '%ct=1%'
so it's gonna grep where ct start with number 1
so kindly try to help me to proceed with select ct when start with number 1 and contain 16 numbers
In its basic form, you might want to use
SELECT column FROM db.table WHERE column REGEXP BINARY 'ct=1[0-9]{15}'
Or, to match as a whole word:
SELECT column FROM db.table WHERE column REGEXP BINARY '[[:<:]]ct=1[0-9]{15}[[:>:]]'
Note that [0-9]{15} matches 15 digits.
The BINARY keyword will make matching case sensitive, so only ct will get matched and CT won't. Remove it if you need to keep the regex case insensitive.
The [[:<:]] matches the left-hand (starting) word boundary and [[:>:]] matches the trailing (end) word boundary.
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
my table has a column with comma-separated (and eventually a space, too) numbers; those numbers can have from five to twelve digits.
9645811, 9646011,9645911, 9646111
or
41031, 41027, 559645811, 5501006009
I need to select the rows with that column containing a number STARTING with given digits. In the above examples, only the first has to be selected. What I've tried so far:
SELECT myfield FROM mytable
WHERE myfield REGEXP ('(^|[,\s]+)(96458[\d]*)([,\s]*|$)');
However the query returns no results. I'd like to select only the first row, where there is a number STARTING with 96458.
Any help would be appreciated :)
You need to use a starting word boundary [[:<:]]:
SELECT myfield FROM mytable WHERE myfield REGEXP ('[[:<:]]96458');
See the MySQL regex syntax for more details.
[[:<:]], [[:>:]]
These markers stand for word boundaries. They match the beginning and end of words, respectively.
See this SQL fiddle.