I have a table, where one of the columns contains city name.
Most cities are OK, but some may have abbreviations with or without a period
Ft. Worth
Ft Worth
If I have a value to Without a period Ft Worth how can I query the table to find all matching cities?
Use a regular expression to make the period optional:
WHERE city RLIKE 'Ft[.]? Worth'
If you want to ignore all periods in the table when comparing with a search string:
WHERE REPLACE(city, '.', '') = 'Ft Worth'
I might be inclined to use replace():
select replace(city, '.', '') = 'Ft Worth'
One nice aspect of this approach is that you can use in:
select replace(city, '.', '') in ('Ft Worth', 'Gun Barrel City', 'Kermit')
However, I would be suspicious and think that there might be other problems. For instance, you might have "Fort Worth".
In any case, you should clean the data:
update t
set city = 'Ft Worth'
where city = 'Ft. Worth';
or
update t
set city = replace(city, '.', '')
where city like '%. %';
Related
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
I would like to get specific value before specific character from specific column.
For example ..
In town column i want only string value before - character , I mean only need ABBEYARD from town column.
I have used following query but not work.
SELECT * FROM `locations` WHERE town = SUBSTRING_INDEX('ABBOTSFORD','-',1)
Note: I only need in WHERE Clouse.
I think you want:
WHERE SUBSTRING_INDEX(town, '-', 1) = 'ABBOTSFORD'
However, I would recommend writing this as:
WHERE town LIKE 'ABBOTSFORD-%'
This can actually take advantage of an index.
Also, it looks like your data might have spaces around the '-'. If so, those should be in the comparison strings as well.
WHERE SUBSTRING_INDEX(town, ' - ', 1) = 'ABBOTSFORD'
WHERE town LIKE 'ABBOTSFORD -%'
You can repeat SUBSTRING_INDEX() in the SELECT to
get only the town or the number that follows.
If you want to return the town names because in the column town they are concatenated with the postcode, then you need to modify the select and not the where part:
SELECT id, TRIM(SUBSTRING_INDEX(town, '-', 1)) townname, postcode, state FROM locations
and maybe add this where part:
WHERE TRIM(SUBSTRING_INDEX(town, '-', 1)) = 'ABBOTSFORD'
In my MySQL database I've some strings like this:
I'm something, Infos S. 12
The pattern I want to search for is: , Infos S., than a number (only digits) and than string end. It's case-insensitive.
How can I search for it and remove it?
I have this I'm something, Infos S. 12 and I want I'm something.
This is what I have so far:
UPDATE my_table SET title_col = REPLACE(title_col, SUBSTRING(title_col, LOCATE(', Infos S. ', title_col), LENGTH(title_col) - LOCATE(')', REVERSE(title_col)) - LOCATE(', Infos S. ', title_col) + 2), '') WHERE title_col LIKE '%(%)%';
How to do the rest?
Edit:
If there's another comma it should get ignored.
Means: I'm, something, Infos S. 12 (note the comma after I'm) should get I'm, something.
You can use regexp to check if the column has the specified pattern and then use substring to update the string with substring upto the ,.
UPDATE my_table
SET title_col = SUBSTRING(title_col,1,locate(', Infos',title_col)-1)
WHERE title_col regexp ', Infos S\\. [0-9]+$'
regexp match is case-insensitive by default. If you want to make it case-sensitive, use regexp binary.
where title_col regexp binary ', Infos S\\. [0-9]+$'
I have troubles with doing such sql query for mysql db:
I need to update field A in my db, but also i have B field, which contains much data, for example:
ASIAN HORSE 70з рус 600A (261x175x220)
or
Бэрен polar 55/59з (555112) 480A (242x175x190)
i must fetch 70з and set it in field A, and 55/59з same (but for another record).
But how can i search in B field something what end's with з but is word (not all data as % before з)
I know, that it could sound like homework... but i real don't know ho to select only word with some end...
The MySQL function substring_index can be used to select pieces of a string delimited by something. For example this picks out the third "word" from MyColumn:
select substring_index(substring_index(MyColumn, ' ', 3), ' ', -1) from MyTable
(70з is the third "word" in ASIAN HORSE 70з рус 600A (261x175x220).)
Update If instead of the third word you are looking for the "word" that ends with 'з', you can use:
select substring_index(substring_index(MyColumn, 'з', 1), ' ', -1) from MyTable
This will consider 'з' as the delimiter though, and removes it from the result. You can add it back with concat:
select concat(substring_index(substring_index(MyColumn, 'з', 1), ' ', -1), 'з') from MyTable
If you are trying to parse the field so the third value always goes into a particular field, then you have a hard problem and probably want to create a user-defined function.
However, if you just want to see if 70з is present and set another field, then this should work:
update t
set B = '70з'
where A like '% 70з' or A like '% 70з %' or A like '70з %' or A = '70з'
This uses spaces to define the word boundaries and considers whether the string is at the beginning, in the middle, at the end, or the entire value in A.
I have a column named streetaddress that contains
<Street Number> <Street Name>
for example:
15 rue Gontier-Patin
4968 Hillcrest Circle
how can i remove the numbers from the beginning of the row purely in sql?
How about something like this - trim off everything up to and including the first space in strings which start with a number
UPDATE mytable
SET addresscol=SUBSTRING(addresscol, LOCATE(' ', addresscol)+1)
WHERE addresscol REGEXP '^[0-9]';
This is based on #Paul Dixon above but is just for returning results without updating the data for things like sorting:
SELECT IF(address REGEXP '^[0-9]', SUBSTRING(address, LOCATE(' ', address)+1), address) AS addrSort FROM table;
MySQL does not have regexp replace functions so it depends on the values in your column. If the string always begins with a number and then a space, you can do it with the SQl query Paul Dixon posted (doh, he was faster than me :D ).
If you need regular expressions to solve it, you have to do it in application code outside the database.
I think this should do the job on the basis that each entry has it's street address seperated from the house number by a space (" ");
UPDATE table
SET streetaddress = MID(streetaddress, LOCATE(' ', streetaddress) + 1);
I've tested this and it works fine.
You can also use the following if you'd like to extract the house number to a new column previous to the update;
UPDATE table
SET housenumber = MID(streetaddress, 1, LOCATE(' ', streetaddress) - 1);