MySQL Finding the End Position of String within String - mysql

I'm looking for a way to identify the end position of a matched string within a string. If I have the following text in a field:
The book is on the shelf
And I want to find the ending position of the text "on", I can find the start using locate(), which returns the starting position, but I'm trying to get the ending position as well.
My expected result then would be something like this:
Text | End Position
----------------------------------------------
The book is on the shelf | 14
I have a query that uses a LIKE to find matching text with wildcards. I'm searching across a field that can contain 5000 characters of text, and of course trying to find the matching text in this field is time consuming. Therefore, I was hoping there is a way to basically show where the LIKE has matched the text. Maybe both the start and ending positions.

SELECT #a := 'The book is on the shelf',
LOCATE('on', #a) AS pos_initial,
(LOCATE('on', #a) + LENGTH('on')) as pos_final;

Related

Find certain information within array

So I’m trying to set where the user adds one line of text containing the Name(Last, First Middle) as well as id number, ssn, and birthday. Example: Doe, John Hank 12345678901 123-45-6789 Jan. 01, 1801
I first split it by using “ “ to break it up into the array. I then need to find which array value has the ssn in it. Because the name can change from Sr, Jr, no middle name, etc. the array value can’t be static at like ssn = eachpart(3). Is there a way to search each piece of the array with the criteria for a mask of a ssn?
You could create and array using Split:
Parts = Split(Input, " ")
Then loop this checking for
IsNumeric(Parts(i))
That found will be the ID.
The next will be the SSN.

How to segregate a single value from a query in MS Access

My apologies but I am a bit lost in the world of Microsoft Access. So I currently have a query that returns a column of values. The output looks like this:
SMABranchCode
Code1
Code2
Code3
etc.
This query then gets moved to a form text box. To do this, I have used Control Source like so:
=IIf(IsNull(DLookUp("SMABranchCode","PWS_SM_QUERY"))," ","X")
This works "Ok"; however there are going to be probably 30+ more text boxes going horizontally next to this one all running the same query. Above them is a label with the Code. So basically when it spits out the data it looks like this (as long as there is an entry for that code):
Code 1 | Code 2 | Code 3
X | X | X
The problem is, I either need to go and create a query for each of these text boxes so that it only pulls a single entry for each Code OR I can hopefully change up my Control Source entry (=IIf(IsNull(DLookUp("SMABranchCode","PWS_SM_QUERY"))," ","X")) to include criteria so that each text box has its own Criteria, so that from the one original query, each Code (1,2,3) only displays its information.
I have tried changing DLookUp to have criteria which has looked like this:
=IIf(IsNull(DLookUp("SMABranchCode","PWS_SM_QUERY","SMABranchCode = Code1"))," ","X")
However that gives me an #Error! in the boxes I apply it too. I'm hoping maybe someone can help me figure out a solution since my brain is turning to mush over this.
Thank you
I presume SMABranchCode is a text type field. Criteria values for text fields must be delimited with apostrophes, date/time field values would use #.
"SMABranchCode = 'Code1'"

Need to delete part of string from one word to the end in MySQL

OK I have trouble and I dont know where else to look. I have a database with a table called "tasks". In this is a column called "description" with fields involving strings.
These descriptions are pretty long, and each one has this format:
1 2 3
Unique description "But I'd like to remind you..." Unique text
I need to delete the second part from every description to the end of the string.
So I need an sql query that will delete only the part of each field that starts with "But I'd like to remind you..." until the end of the string, leaving ONLY part 1, the unique description.
The SUBSTRING_INDEX function is good for this sort of thing:
SELECT SUBSTRING_INDEX(description, ' "But I''d', 1)
FROM tasks
The second argument is the string you're looking for. The third argument of 1 means return everything before the first occurrence of the string you're looking for.
I've included just the first part of the But I'd like to remind you... text. You can include as much of it as needed to make sure you get a proper hit.
The query above will return the first part of the description. To update the table and remove everything from But I'd like to remind you... to the end, just put the expression into an UPDATE:
UPDATE tasks
SET description = SUBSTRING_INDEX(description, ' "But I''d', 1)

MySQL Behaviour of Wildcard LIKE matching

I have the following data in TableA...
ID | Text
---------------------------------------------
1 | let's find this document
2 | docments are closed
...and if I do the following select...
select Text from TableA where Text like '%doc%';
...I seem to get a strange result. Both rows are returned. With this select, should it not only return row 1? I would have thought that..
select Text from TableA where Text like 'doc%';
...would have returned just row 2. Am I missing something?
What I'm trying to do is run 3 separate searches across this data as part of my searching tool. The first match is to look for the specified pattern "doc" at the beginning of a string, secondly, my next match looks for the same pattern but at the end of a string, and thirdly, identify if the pattern appears anywhere within the text - so can have text surrounding it. Ideally, the first search would only match row 2, the second search would return no results and the third result would only return row 1.The reason for doing it like this is I wanted to try and get a feel for how the pattern matched the string. Would make it easier to read the results to know that the pattern for a given row matched either (a) at the beginning, (b) at the end, (c) anywhere in the middle.Had thought about using regexp, but my data is unicode.
No, the first query returns both rows, because % means 0 or more characters. So if doc is the first thing appearing in the field, it matches the %doc% pattern as well.
But you're right on the second query, it will only return row 2.
doc_% should match it at the beginning, having at least one character after it.
%_doc should match it at the end, having at least one character before it.
%_doc_% should match it anywhere, having at least one character before and after it.
Note that these strict criteria fail to find the exact string "doc", i.e. with nothing before or after it. You may want to include this case in, say, query #1, by loosening it:
doc% should match it at the beginning, having any number of characters after it.

MySQL - Populate a field based on part of another field

I have a table where one column, named Description, has a variety of information all crammed together. Part of that information is an abbreviation for a vehicle maker. For example, the Description column could read "6cylM.Benz32zy,.026L"
I want to populate a new Vehicle column with Mercedes Benz wherever the Description column contains M.Benz somewhere inside. The key word there is "somewhere"; the abbreviated vehicle name could be anywhere in the description field.
So far I have the following code. However, MySQL tells me it can't find the %M.Benz% column. I'm not trying to look for a column named %M.Benz%, I'm trying to find TEXT that is %M.Benz%.
UPDATE tbl_arcore_repuestos
SET Vehicle = `Mercedes Benz`
WHERE Description LIKE `%M.Benz%`;
Any ideas? Most of the other solutions on StackOverflow to similar questions rely on the sought-after text being in a fixed position (like at the beginning of the string or separated by a consistent character). The text I'm looking for could be ANYWHERE in the Description column.
Have you missed the quotes or used backticks instead of quotes?
UPDATE tbl_arcore_repuestos SET Vehicle = 'Mercedes Benz' WHERE Description LIKE '%M.Benz%';