how to find a comma with mysql find_in_set - mysql

is it possible to find a commma with find_in_set?
I tried:
select find_in_set('\,', '\,');
my use case is:
where find_in_set(r.tag, vfilterList)
here, vfilterList is a comma separated list provided as input to a stored procedure - and r.tag is the tag string in the table. (So I want to filter to only rows that have a tag that's in the vfilterList). However some tag strings have commas in them. (in vfilterList the commas in tags would be escaped?).
Something tells me i'm doing it wrong?

No, it isn't possible.
The documentation specifically points out that this doesn't work.
This function does not work properly if the first argument contains a comma (“,”) character.
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_find-in-set
The function is designed to return the ordinal position of a substring, delimited by commas. By definition, essentially, it would never find anything with a comma in it, since it's only considering the values between them.
Depending on what you're actually trying to accomplish, INSTR() or SUBSTRING_INDEX() might be useful.

Ended up just replacing commas with pipes. (and doing the same for each tag within the filter list). It's reasonably accurate with false positives very unlikely for our use case.
where find_in_set(replace(r.tag,",","|"), vfilterList)

Related

replacing special characters in recordset access vba

I'm using input tables with special chars, but my output tables need to be without them.
So i need function that would replace special chars in query or table
for example
Š=S Č=C Ć=C Ž=Z
Any sugestions?
You could pop them into a string, and then use Replace, e.g. strYourString = Replace(strYourString, "Š","S").
Downside of this approach is you'd need to specify all the special characters you want to replace, and run the code several times, perhaps as a loop.
Replace function does that but (as far as I know) for single character:
Expr1: Replace([Employees].[Prezime];"č";"c")
For a fast but not efficiant solution you can create several fileds named Expr1, Expr2 Expr3, and in each remove one special character.
But I am searching for more "decent" solution.

mysql selecting multiple values wrapped in identical tags

I don't know if there is a better description of my problem, but here is what I need help with:
I have a field with lots of data, and the part I need to solve looks like this:
::field_x::<br />||field_x||519||/field_x||<br />||field_x||281||/field_x||<br />::/field_x::
I have to extract each number (id) from this, 519 and 281 in this example, and insert them in a field in another table, separated by spaces or commas. I know how to use SUBSTRING - LOCATE method, but that would return only the first instance, so is there a method to extract them all in one go?
SUBSTRING INDEX LOCATE will work. There is no built in functionality for regular expressions so unless you handle it before it gets to mysql...you're stuck using the SUBSTRING INDEX LOCATE method...
If you need to iterate through a dataset, you will need to initiate a cursor, or FOR loop and use a stored proc.
parse results in MySQL via REGEX

Using REGEX to alter field data in a mysql query

I have two databases, both containing phone numbers. I need to find all instances of duplicate phone numbers, but the formats of database 1 vary wildly from the format of database 2.
I'd like to strip out all non-digit characters and just compare the two 10-digit strings to determine if it's a duplicate, something like:
SELECT b.phone as barPhone, sp.phone as SPPhone FROM bars b JOIN single_platform_bars sp ON sp.phone.REGEX = b.phone.REGEX
Is such a thing even possible in a mysql query? If so, how do I go about accomplishing this?
EDIT: Looks like it is, in fact, a thing you can do! Hooray! The following query returned exactly what I needed:
SELECT b.phone, b.id, sp.phone, sp.id
FROM bars b JOIN single_platform_bars sp ON REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(b.phone,' ',''),'-',''),'(',''),')',''),'.','') = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(sp.phone,' ',''),'-',''),'(',''),')',''),'.','')
MySQL doesn't support returning the "match" of a regular expression. The MySQL REGEXP function returns a 1 or 0, depending on whether an expression matched a regular expression test or not.
You can use the REPLACE function to replace a specific character, and you can nest those. But it would be unwieldy for all "non-digit" characters. If you want to remove spaces, dashes, open and close parens e.g.
REPLACE(REPLACE(REPLACE(REPLACE(sp.phone,' ',''),'-',''),'(',''),')','')
One approach is to create user defined function to return just the digits from a string. But if you don't want to create a user defined function...
This can be done in native MySQL. This approach is a bit unwieldy, but it is workable for strings of "reasonable" length.
SELECT CONCAT(IF(SUBSTR(sp.phone,1,1) REGEXP '^[0-9]$',SUBSTR(sp.phone,1,1),'')
,IF(SUBSTR(sp.phone,2,1) REGEXP '^[0-9]$',SUBSTR(sp.phone,2,1),'')
,IF(SUBSTR(sp.phone,3,1) REGEXP '^[0-9]$',SUBSTR(sp.phone,3,1),'')
,IF(SUBSTR(sp.phone,4,1) REGEXP '^[0-9]$',SUBSTR(sp.phone,4,1),'')
,IF(SUBSTR(sp.phone,5,1) REGEXP '^[0-9]$',SUBSTR(sp.phone,5,1),'')
) AS phone_digits
FROM sp
To unpack that a bit... we extract a single character from the first position in the string, check if it's a digit, if it is a digit, we return the character, otherwise we return an empty string. We repeat this for the second, third, etc. characters in the string. We concatenate all of the returned characters and empty strings back into a single string.
Obviously, the expression above is checking only the first five characters of the string, you would need to extend this, basically adding a line for each position you want to check...
And unwieldy expressions like this can be included in a predicate (in a WHERE clause). (I've just shown it in the SELECT list for convenience.)
MySQL doesn't support such string operations natively. You will either need to use a UDF like this, or else create a stored function that iterates over a string parameter concatenating to its return value every digit that it encounters.

MySQL: Formatting a string

My database contains a string pattern that is used to allow for easy user editing via a JS-script.
The string is basicly formatted like so:
aaa[bbb#ccc]ddd[eee#fff]ggg
the result I am looking for is
aaacccdddfffggg
I'd like to do this when selecting the string from the database. I'm guessing a regex should do the trick. But my knowledge in the subject of regex's is rather limited. However, this is not a requirement, if there exist a more elegant solution to the problem.
Unfortunately, you can only use a MySQL REGEXP in a WHERE clause, to match against values. You can't use them to transform Strings.
You'll either need to do it clientside, or work with the other String Functions. A MID() would do the trick, if the lengths and positions of the substrings are fixed. If not, use POSITION() (or LOCATE()) to find the special characters []#.

Regexp MySql- Only strings containing two words

I have table with rows of strings.
I'd like to search for those strings that consists of only
two words.
I tried few ways with [[:space:]] etc but mysql was returning
three, four word strings also
try this:
select * from yourTable WHERE field REGEXP('^[[:alnum:]]+[[:blank:]]+[[:alnum:]]+$');
more details in link :
http://dev.mysql.com/doc/refman/5.1/en/regexp.html
^\w+\s\w+$ should do well.
Note; what I experience more often in the last days is that close to nobody uses the ^$-operators.
They are absolutely needed if you want to tell if a string starts or ends with something or want to match the string exactly, word for word, as you. "Normal" strings, like you used (I assume you used something like \w[:space]\w match in the string, what means that they also match if the condition is true anywhere within the string!
Keep that in mind and Regex will serve you well :)
REGEXP ('^[a-z0-9]*[[:space:]][a-z0-9]*$')