How to use locate function for string? - mysql

How to use locate function for string if string is present in database field?
I want to find location of "m/l" such string. below query return 0 to me however it is present in database table
SELECT locate('"m\/l":"',field) FROM table_name.

If you're dealing with escaped strings, you may need to crank up the escaping significantly to get it to work. Backslashes are special characters and will be collapsed down.
You may want to try:
LOCATE('"m\\/l":"',field)
LOCATE('"m\\\\/l":"',field)
If this is located inside another string, or two levels of string (e.g. inside JSON itself), you may need a lot of them.

Related

MySQLAdmin replace text in a field with percent in text

Using MySQLAdmin. Moved data from Windows server and trying to replace case in urls but not finding the matches. Need slashes as I don't want to replace text in anything but the urls (in post table). I think the %20 are the problem somwhow?
UPDATE table_name SET field = replace(field, '/user%20name/', '/User%20Name/')
The actual string is more like:
https://www.example.com/forum/uploads/user%20name/GFCI%20Stds%20Rev%202006%20.pdf
In a case you are using MariaDB you have REGEXP_REPLACE() function.
But best approach is to dump the table into the file. Open it in a Notepad ++
and run regex replace like specified on a pic:
Pattern is: (https:[\/\w\s\.]+uploads/)(\w+)\%20(\w+)((\/.*)+)
Replace with: $1\u$2\%20\u$3$4
Then import the table again
Hope this help
If its MariaDB, you can do the following:
UPDATE table_name SET field = REGEXP_REPLACE(field, '\/user%20name\/', '\/User%20Name\/');
First, please check, what is actually stored in the database: %20 is a html-entity which represents a whitespace. Usually, when you are storing this inside the database, it will be represented as an actual whitespace (converted before you store it) -> Hence your replace doesn't match the actual data.
The second option that might be possible - depending on what you want to do: You are seeing the URL containing %20, therefore you created your database records (which you would like to fetch) with that additional %20 - And when you now try to query your results based on the actual url, the %20 is replaced with an "actual" whitespace (before your query) and hence it doesn't match your stored data.

procedure which does string comparison in mysql

My requirement is some thing like this. If I want to search a string "Bangalore" in the table. But the in the table their are some leading spaces and trailing spaces before and after the string. How to write a procedure which helps me to search the string in mysql. I want to search the string during the procedure call
Use pattern matching? I.e. where value like "%bangalore%"
This should suffice to retrieve the data as is, but I would highly recommend trimming the whitespace out of the data, as suggested above.

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

is there any way to import NULLS to mySQL inside of single quotes?

I'm using VBA that encloses every field with single quotes in order to import into mySQL and noticed that every variation I tried on NULL for numeric data types imported as 0. If I drop the backticks the nulls import fine, so I think my question boils down to this: if there any way to indicate NULL data inside of single quotes in a mySQL import?
this website shows a bunch of examples, but nothing inside of single quotes.
thanks!
If the string inside the single quotes is escaped, too, then there is no way of making it NULL. That would negate the purpose of quotes+escaping.
If it is not escaped, you could "break out" of the single quotes with a single quote. For example if you store the string '/0+' it will become ''/0+'' (say: empty string divided by zero plus empty string) which is NULL because anything divided by zero is NULL.

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 []#.