I have a string field, FieldS, that can be 2-10 words long. I can easily find all fields that contain word 'X' but what I am looking for is to extract all unique word pairs with 'X NextWord'. Clearly I can do
Select FieldS from Table1 where FieldS like '% X %'
and I'm thinking somehow there is a substring_index involved here but I can't construct if after several tries so thought perhaps there is a more relevant function I am unaware of.
You can do this with substring_index() -- twice.
Here is one way:
select concat(X, ' ',
substring_index(substring_index(field5, 'X', -1), ' ', 1)
)
Notes:
This assumes that 'X' only occurs once in the string. Or, more accurately, it uses the last occurrence of 'X' in the field.
This assumes that the only word break character is space.
EDIT:
If you are concerned about the spaces around the X you can use:
select concat(X, ' ',
substring_index(substring_index(concat(' ', field5, ' '), ' X ', -1), ' ', 1)
)
Related
I have columns with numeric values like this:
14.333,67
3.123,90
1.234.222,01
and so on. i've written a small statement that will convert the periods to commas via the REPLACE function.
ie
UPDATE table SET column = REPLACE (column, '.', ',')
now what i need to do is update that last comma value to be a period. the datatype is char and i need to keep it that way.
I'm trying to use the RIGHT function like this.
REPLACE (RIGHT(column, 3), ',', '.')
which only seems to pull the last three values. For example after I run these two statements I get the following results in order:
original: 14.333,67
first update: 14,333,67
last update: .67
how can i squeeze all of this into one update/set statement to get the full value?
Try to chain few REPLACEs. You will need three in one row due to you have to temporarily replace one of your replaced characters to something which doesn't match to the second replaced character.
SELECT
REPLACE(
REPLACE(
REPLACE(IFNULL('123.456,78', ''),
',', ';'),
'.', ','),
';', '.') as result;
result
123,456.78
So, your update query will be like:
UPDATE
table
SET
column = REPLACE(
REPLACE(
REPLACE(IFNULL(column, ''),
',', ';'),
'.', ','),
';', '.')
You can concatenate the rest of the string with the part you're replacing in.
SET column = CONCAT(LEFT(column, LENGTH(column)-3), REPLACE(RIGHT(column, 3), ',', '.'))
I would like to search inside 2 columns for parts of a string. It's a backwards way of searching so I doubt it's possible.
I think part of it would have something to do with:
REPLACE( event_name, ' ', '')
and
REPLACE( venue_name, ' ', '')
to get rid of the space.
I also thought REGEX might come into it.
Absolutely no idea where to start! PSUEDOCODE might be:
CONCAT(
event_name = REPLACE( part of :search, ' ', '')
,
venue_name = REPLACE( part of :search, ' ', '')
)
= :search
If I used noddybobstheatre as the search term I want to search the column event_name for part of it and venue_name for another part and when the 2 parts are put together they equal the search term.
event_name = 'noddy'
venue_name = 'bobs theatre'
Like I said, this might be a crazy ask...
Reading what you need I thought that the like statement should use the column and not the search value.
here is my reproduction of what I understood from your problem
CREATE TABLE movies
(`word1` varchar(5), `word2` varchar(7))
;
INSERT INTO movies
(`word1`, `word2`)
VALUES
('micko', 'to'),
('he', 'llbrane'),
('mick', 'oto'),
('hell', 'brane')
;
then here is the sql statement that I used
select * from movies where 'mickoto' like CONCAT(Replace(word1,' ',''),'%') and 'mickoto' like CONCAT('%',Replace(word2,' ',''))
all you have is to adapt this to your context
here is the fiddle execution
use this,
concat(replace(event_name,' ',''),replace(venue_name,' ',''), )=:search
There are strings in table
+1-123-456-7890
11234567890
+1 1234567890
1777555999
I have variable $phoneNumber. I want to pass variable in sql query to get matching records.
example:
$phoneNumber holds the value 1234567890
and using same variable I want to fetch following records in one query
+1-123-456-7890
11234567890
+1 1234567890
I want to select both records in one query.
how can I match data (11234567890) with above saved numbers?
We can do this following the replacement logic already suggested here combined with regular expressions, to match only the phone numbers with the format you want to target:
WITH yourTable AS (
SELECT '+1-123-456-7890' AS phone UNION ALL
SELECT '+1 1234567890' UNION ALL
SELECT '+6512345678'
)
SELECT
phone,
REPLACE(REPLACE(REPLACE(phone, ' ', ''), '+', ''), '-', '') AS common_phone
FROM yourTable
WHERE phone REGEXP '\\+[0-9][[:space:]-][0-9]{3}-?[0-9]{3}-?[0-9]{4}';
Demo
Try this !
Regex :
[\+]?[0-9](?:[-| ])?\d{3}[-]?\d{3}[-]?\d{4}
Verify through regxe101:
If both $phonenumber and your string column could (potentially) both have the troublesome characters, this is hard to solve with regular expressions.
where replace(replace(replace($phonenumber, ' ', ''), '-', ''), '+') = replace(replace(replace(phonecol, ' ', ''), '-', ''), '+')
I solved this with using replace() in query.
I removed special characters from variable $phoneNumber (not through sql but before passing varable to sql) and compared it with the string result of the replace. SELECT phone FROM myTable where REPLACE(REPLACE(REPLACE(phone, ' ', ''), '+', ''), '-', '') = $phoneNumber;
Rethink the overall design.
Cleanse phone numbers before putting them into the table. Then that SELECT, and the other SELECTs that are yet to be written, will be simple.
I have a varchar(255) column with FULLTEXT index. I need a query to get the most frequent words in the entire column as
Word Frequency
key1 4533
key2 4332
key3 2932
Note 1: I would prefer to skip common words such as prepositions, but it is not critical as I can filter them later. Just mentioned if it can speed up the query.
Note 2: It is a table with over a million rows. It is not a regular query but should be practically fast.
If you even give a hint how the query should look like, it will be a great help.
This is not really something that is easy to do in MySQL. The full text index is not available for querying. One thing you can do is extract words. This is a bit painful. The following assumes that words are separated by a single space and gets the frequencies of the first three words:
select substring_index(substring_index(t.words, ' ', n.n), ' ', -1) as word, count(*)
from t cross join
(select 1 as n union all select 2 union all select 3
) n
on n.n <= length(t.words) - length(replace(t.words, ' ', '') + 1
group by substring_index(substring_index(t.words, ' ', n.n), ' ', -1)
order by count(*) desc;
my brain might be really tired but i coudn't seem to work my head around this simple query.. I want to extract a last substring within delimiters such as ',' from a column? i was thinking about using REVERSE or RIGHT.. but the results are coming off as really bizarre...
Lets say i have a table column 'DESCRIPTION' in a table LOANS with an entry
Changed Loan Laptop from "IT-X130E-10" to "IT-X130E-9".
and i want the last substring
IT-X130E-9
within delimiters '"' hope its clearer now
Can try SUBSTRING INDEX(str,delim,count)
SELECT SUBSTRING_INDEX(
SUBSTRING_INDEX('yourString', '"', -2), '"', 1);
The key to this is reverse() and some brute force:
select replace(replace(right(val, instr(substring(reverse(val), 3, 100), '"')+2), '"', ''), '.', '')
from (select 'Changed Loan Laptop from "IT-X130E-10" to "IT-X130E-9".' as val) t
This assumes that the string does not contain either '"' or '.'.