i have a notes column which contains text and has an id within the text, something like
"some random text (actvityid - 1234)"
i need to pull out the id 1234 in this case and update the activityid column within the same table.
my query looks like this
"UPDATE table_name SET activityId = {$f['activityId']} WHERE notes REGEXP '{$f['activityId']}' "
the problem with this is if $f['activityId'] is 34 or 123 for example it still updates the activityid column with that value. How can i do an exact match on "1234" and update only if it matches the whole string, here "1234".
Many thanks.
WHERE notes REGEXP CONCAT('(actvityid - ', {$f['activityId']}, ')')
or
WHERE notes REGEXP '[[:<:]]{$f['activityId']}[[:>:]]'
[[:<:]] and [[:>:]] stands for word boundaries.
No need to use CONCAT if variable is passed from PHP,
and no need to use REGEXP if you match exact string without special characters
WHERE notes LIKE '%(actvityid - {$f['activityId']})%'
Related
I try to find query to find a string that 2nd character and 2nd last character both are letter m.
SELECT last_name
FROM employees
WHERE (last_name LIKE '_m%m_' AND LENGTH(last_name) >= '3');
Thanks in advance :)
Why not just OR instead of AND? I don't see the point of AND when your LIKE operator allready rules out names below three characters. You don't need to use regex nor a check for length:
SELECT last_name FROM employees
WHERE last_name LIKE '_m_'
OR last_name LIKE '_m%m_';
The use of OR and LIKE does catch any string that has at least 3 characters.
If you must use regex, try REGEXP operator:
SELECT last_name FROM employees WHERE last_name REGEXP '^.m(.*m)?.$';
Where the pattern means:
^.m - Start-line anchor with a single character and a literal 'm';
(.*m)? - Optional capture group to match 0+ characters upto a literal 'm';
.$ - A single character with end-line anchor.
The benefit of REGEXP is that it's a bit less verbose if you need case-insensitive matching using pattern: '^.[Mm](.*[Mm])?.$'. See an online demo.
If you need all record with second and last character is m you can use the following query:
select * from <table> where <column> like '_m%m'
the _ in the query is a placeholder for one character and % for many characters
Hello I have made a dummy table that I am practicing with and I am trying to get the lasts name first letter for example. Aba Kadabra and Alfa Kadabra the last letter of their last name is 'K' so when I was testing some queries such as...
select * from employees
where full_name like 'K%'
select * from employees
where full_name like 'K%'
Neither of these worked. Can anyone tell me the best way to accomplish this?
Because % works that way. See here
So, 'K%' just brings all full_name that start with K.
and '%K' brings all full_name that end with K.
What you need is '% K%', test it please.
MySQL LIKE operator checks whether a specific character string matches
a specified pattern.
The LIKE operator does a pattern matching comparison. The operand to
the right of the LIKE operator contains the pattern and the left hand
operand contains the string to match against the pattern. A percent
symbol ("%") in the LIKE pattern matches any sequence of zero or more
characters in the string. An underscore ("_") in the LIKE pattern
matches any single character in the string. Any other character
matches itself or its lower/upper case equivalent (i.e.
case-insensitive matching). (A bug: SQLite only understands
upper/lower case for ASCII characters by default. The LIKE operator is
case sensitive by default for unicode characters that are beyond the
ASCII range. For example, the expression 'a' LIKE 'A' is TRUE but 'æ'
LIKE 'Æ' is FALSE.)
You can use below query:
select * from table where full_name like '% K%'
I'm trying to find if any word of a given string is contained within a column in a mySQL table.
Example
String: 'Company LLC'
Column: 'LLC'
I've tried the below query but no dice.
select * from table
where column sounds like '%Company LLC%'
Try this:
select * from table
where column rlike replace('Company LLC', ' ', '|')
This does a regex match. In regex, the pipe char | means "or". The resulting regex means "Company or LLC". In MySQL, rlike matches when any part of the value matches (rather than the whole column matches), so you don't need ".*" on each end.
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 a SELECT query in MYSQL that will retrieve all rows in one table witch field values contain "?" char with one condition: the char is not the last character
Example:
ID Field
1 123??see
2 12?
3 45??78??
Returning rows would then be those from ID 1 and 3 that match the condition given
The only statement I have is:
SELECT *
FROM table
WHERE Field LIKE '%?%'
But, the MySQL query does not solve my problem..
The LIKE expressions also support a wildcard "_" which matches exactly one character.
So you can write an expression like the example below, and know that your "?" will not be the last character in the string. There must be at least one more character.
WHERE intrebare LIKE '%?_%'
Re comment from #JohnRuddell,
Yes, that's true, this will match the string "??" because a "?" exists in a position that is not the last character.
It depends whether the OP means for that to be a match or not. The OP says the string "45??78??" is a match, but it's not clear if they would intend that "4578??" to be a match.
An alternative is to use a regular expression, but this is a little more tricky because you have to escape a literal "?", so it won't be interpreted as a regexp metacharacter. Then also escape the escape character.
WHERE intrebare REGEXP '\\?[^?]'
you can just add an additional where where the last character is not a ?
SELECT *
FROM intrebari
WHERE intrebare LIKE '%?%' AND intrebare NOT LIKE '%?'
you could also do it like this
SELECT *
FROM intrebari
WHERE intrebare LIKE '%?%' AND RIGHT(intrebare,1) <> '?'
DEMO