Is it possible to use a wildcard like # to match items that start with a number in a IN-clause
The following works:
Criteria: In ("2T","2TF","2TC","2TO","2TFC","2TOC","2C","2CO","2FC")
However I'd like to catch also instances like "3TF" and "6TF"
But obviously the following does not work
Criteria: In ("T","#TF","#TC","#TO","#TFC","#TOC","#C","#CO","#FC")
Is there a way to handle this
UPDATE [attempt trying proposal of Erik A]
Assuming that you are trying to match any of those suffixes for a record with any single integer prefix, you could use a combination of like and in with the first character removed using mid, e.g.:
myfield like "#*" and
mid(myfield, 2) in ("T","TF","TC","TO","TFC","TOC","C","CO","FC")
Or alternatively, using only like:
myfield like #T or
myfield like #C or
myfield like #T[FCO] or
myfield like #T[FO]C or
myfield like #CO or
myfield like #FC
Here, T[FCO] matches TF, TC, and TO; and similarly, T[FO]C matches TFC and TOC.
You can't. Add Like:
Where
Field In ("2T","2TC","2TO","2TFC","2TOC","2C","2CO","2FC") Or
Field Like "#TF"
You can't, without VBA, but with VBA it's quite easy:
Declare a function to do the matching:
Public Function InLike(DatabaseField As Variant, ParamArray LikeConditions() As Variant) As Boolean
Dim v As Variant
For Each v In LikeConditions
If DatabaseField Like v Then
InLike = True
Exit Function
End If
Next
End Function
Then use it:
InLike (MyField, "T","#TF","#TC","#TO","#TFC","#TOC","#C","#CO","#FC")
Note that the first argument is the field, so it handles a little differently. Also, because it's VBA it carries a performance and compatibility impact.
Related
I have this query:
UPDATE tbl SET fldname = replace(fldname,'St','Stats');
However if anything begins with the letters St, such as Stump, it would then look like Statsump. How can I just have the St replaced with the exact match? Thanks!
AFAIK MySQL does not directly support regexp replacing (http://bugs.mysql.com/bug.php?id=27389), unless you use user defined functions.
You could try adding a WHERE char_length(fldname) == 5, though - this would match only the strings of length 5, and the only string of length 5 containing 'Stats' is... 'Stats'.
Or of course ditch the whole thing and just do SET fldname='St' WHERE fldname == 'Stats', which would probably be the sane thing to do unless your query is more complex than your example.
I need a function in R that mimics the functionality of LIKE in MySQL.
(I need to validate outcomes of SQL queries and R scripts against each other. If I had a function that exists to mimic the functionality of LIKE, great, that reduces my workload.)
I am adding some of the behaviors of LIKE from the link above. As you can see, there are ways in which LIKE differs from the standard grep regex.
LIKE (description from the link)
Pattern matching using SQL simple regular expression comparison. Returns 1 (TRUE) or 0 (FALSE).
Per the SQL standard, LIKE performs matching on a per-character basis, thus it can produce results different from the = comparison operator:
Trailing spaces are significant
With LIKE you can use the following two wildcard characters in the pattern.
Character Description
% Matches any number of characters, even zero characters
_ Matches exactly one character
In MySQL, LIKE is permitted on numeric expressions. (This is an extension to the standard SQL LIKE.)
mysql> SELECT 10 LIKE '1%';
-> 1
Try sqldf package. You can write sql-like queries on data.frame
For example:
require(sqldf)
data(CO2)
new.data <- sqldf("select * from CO2 where Plant like 'Qn%'")
try ?grepl or package sqldf
df=data.frame(A=c("mytext_is_here","anothertext_is_here","mytext_is_also_here"),B=1:3)
df
firstSolution = subset(df, grepl("^mytext", A))
library("sqldf")
secondSolution = sqldf("select * from df where A like 'mytext%'")
Source: page 8 of http://cran.r-project.org/web/packages/sqldf/sqldf.pdf
I think you could use grepl function in R to do the same.
grepl does partial string matching and it will return a logical vector which you could later use to subset data along with other conditions as well.
You could also later use '!' sign in front of grepl to filter out the results having that expression.
ex. sample=c("data","ddata","ddata1")
filtered_data=grepl("dd",sample)
# it gives a logical vector FALSE TRUE TRUE
#and it can be used as follows to find out all the elements that have a string "dd" in it.
sample[grepl("dd",sample)]
Please note that grepl is case sensitive.
I am trying to use the REPLACE function to search a string and remove a charcter. Here is the code..
SELECT Test.*, Replace([Data],ChrW(10),"",(Len([Data])-2),1) FROM Test;
Although this is just a select stmt, the result set returns the last three characters of data from the column [Data]. Instead of starting the search in that location.
The UPDATE statement here, does the same...
UPDATE Test SET Test.Data = Replace([Data],ChrW(10),"",(LEN([Data])-2))
WHERE (((Test.[Data]) Like ("*" & ChrW(10))));
I would still expect the search to being at the results of LEN([Data])-2. Instead only the last characters are returned. The substitution is successful.
Any help on my oversight here would be appreciated.
Weird. I have confirmed what you're getting. In a query, adding a starting position to the Replace() function acts as a Right() function as well. I can't find any documentation for that. In VBA, it behaves as one would expect, in that it returns the whole string, sans the replaced chars.
You have 3 options for a workaround.
You can replace all Chrw(10), but I think you probably don't want to do that since you specified a start position.
You can use the IIF() statement to test if the last char is Chr(10), and if so, do a Left() function to exclude the last char. Kinda messy.
If you're working in Access, you can create your own function in VBA and call it instead. You can call your function Repl, so it would look something like this: UPDATE Test SET Test.Data = Repl([Data]).
I'm trying to get around pulling all the data from a table, and cycling through it with php. Here's my current Query:
SELECT
*
FROM
ExampleTable
WHERE
StringContains LIKE "%lkjlkjsomeuser#example.comjkjhkjhkjhkjhk,mniu,mk,mkjh%"
ExampleTable.StringContains has values that look like 'someuser#example.com', 'someuser2#example.com', etc.
This doesn't match because LIKE only finds sub strings of the column value, not the other way around. Any ideas on commands to find rows where the table value is a substring of the passed string?
SELECT
*
FROM
ExampleTable
WHERE
'lkjlkjsomeuser#example.comjkjhkjhkjhkjhk,mniu,mk,mkjh' LIKE
CONCAT('%', StringContains, '%')
Try this:
SELECT *
FROM ExampleTable
WHERE "lkjlkjsomeuser#example.comjkjhkjhkjhkjhk,mniu,mk,mkjh" LIKE
CONCAT("%",StringContains,"%")
The key is to recognize that the column variable just represents a string, and the LIKE statement is always comparing two strings in the form
"stringA" LIKE '%stringB%'
Usually people use it to search for a "part" of a string contained in the "whole" database field string, but you can easily switch them. The only extra tool you need is the CONCAT statement, since you want the database field to be the part instead of the whole. The CONCAT statement builds a string with the %'s around the database field string, and the string form of the argument is now equivalent to
"stringB" LIKE "%stringA%"
Just make the LIKE in the opposit order. Since you have to add those % you'll have to concatenate the field first:
SELECT *
FROM ExampleTable
WHERE "lkjlkjsomeuser#example.comjkjhkjhkjhkjhk,mniu,mk,mkjh" LIKE CONCAT('%', StringContains, '%');
I have a question about replacement a particular string in mysql but one part of the string is is changed every time e.g
"(my string to replace 1224:2)"
"(my string to replace 134:4)"
"(my string to replace 1824:9)"
"(my string to replace 14:2)"
I can change first part of string using this query
update dle_post set short_story = replace(short_story,'(my','( my');
but how to replace other parts like 1224:2) , or 14:2) or any other part that ends with a number 1,2,3.. and a ). I can not use bracket ")" because it is used on many other places.
Not the most elegant way, but...
update dle_post
set short_story =
replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(
short_story,
'0)','0 )'),'1)','1 )'),'2)','2 )'),'3)','3 )'),'4)','4 )'),'5)','5 )'),'6)','6 )'),'7)','7 )'),'8)','8 )'),'9)','9 )');
Regular expressions should work in this kind of case. Take a look related question: How to do a regular expression replace in MySQL?