MySQL search to ignore hyphens - mysql

How can I search for "1-800-flowers" by "1800flowers" in MySQL?
I have the data "1-800-flowers", but I want to find it by "1800flowers".

You're probably best off creating a second column that you fill with 1800flowers (replacing all characters you want to ignore) and searching that. That way, you can make full use of indexing.
A quick way to convert all existing data would be
UPDATE table SET columnname_without_hyphens = REPLACE(columnname, "-", "");

If your problem is just ignoring hyphens, I may suggest using REPLACE to eliminate them, like this:
SELECT ... WHERE REPLACE(column, '-', '') ...
Otherwise, if you're looking for strings that "sound alike", you may want to have a look at the SOUNDEX function.

The use of the replace function will kill any ability to use an index on the column, but:
select *
from YourTable
where replace(YourColumn, '-', '') = '1800flowers'

Related

how to handle white spaces in sql

I want to write an SQL query that will fetch all the students who live in a specific Post Code. Following is my query.
SELECT * FROM `students` AS ss WHERE ss.`postcode` LIKE 'SE4 1NA';
Now the issue is that in database some records are saved without the white space is postcode, like SE41NA and some may also be in lowercase, like se41na or se4 1na.
The query gives me different results based on how the record is saved. Is there any way in which I can handle this?
Using regexp is one way to do it. This performs a case insensitive match by default.
SELECT * FROM students AS ss
WHERE ss.postcode REGEXP '^SE4[[:space:]]?1NA$';
[[:space:]]? matches an optional space character.
REGEXP documentation MySQL
Whether case matters depends on the collation of the string/column/database/server. But, you can get around it by doing:
WHERE UPPER(ss.postcode) LIKE 'SE4%1NA'
The % will match any number of characters, including none. It is a bit too general for what you might really need -- but it should work fine in practice.
The more important issue is that your database does not validate the data being put into it. You should fix the application so the postal codes are correct and follow a standard format.
Use a combination of UPPER and REPLACE.
SELECT *
FROM students s
WHERE UPPER(REPLACE(s.postcode, ' ', '')) LIKE '%SE41NA%'
SELECT * FROM students AS ss
WHERE UPPER(REPLACE(ss.postcode, ' ', '')) = 'SE41NA' ;
SELECT *
FROM students AS ss
WHERE UPPER(ss.postcode) LIKE SELECT REPLACE(UPPER('SE4 1NA'), ' ', '%'); ;
I propose using the spaces replaced with the'%' placeholder. Also transform the case to upper for both sides of the LIKE operator

MySql : query to format a specific column in database table

I have one column name phone_number in the database table.Right now the numbers stored in the table are format like ex.+91-852-9689568.I want to format it and just want only digits.
How can i do it in MySql ? I have tried it with using functions like REGEXP but it displays error like function does not exist.And i don't want to use multiple REPLACE.
One of the options is to use mySql substring. (As long as the format doesn't change)
SELECT concat(SUBSTRING(pNo,2,2), SUBSTRING(pNo,5,3), SUBSTRING(pNo,9,7));
if you want to format via projection only, use SELECT, you will only need to use replace twice and no problem with that.
SELECT REPLACE(REPLACE(columnNAme, '-', ''), '+', '')
FROM tableName
otherwise, if you want to update the value permanently, use UPDATE
UPDATE tableName
SET columnName = REPLACE(REPLACE(columnNAme, '-', ''), '+', '')
MySQL does not have a builtin function for pattern-matching and replace.
You'll be better off fetching the whole string back to your application, and then using a more flexible string-manipulation function on it. For instance, preg_replace() in PHP.
Try the following and comment please.
Select dbo.Regex('\d+',pNo);
Select dbo.Regex('[0-9]+',pNo);
Reference on RUBLAR.
So MYSQL is not like Oracle, hence you may just use a USer defined Function to get numbers. This could get you going.

How to remove -XXXX from Zip Code field using MySQL REGEXP?

Can someone give me an example of how to remove anything after a dash "-" in a zip code field using SQL commands?
For example, change any of this:
XXXXX-X
XXXXX-
XXXXX-XX
XXXXX-XXXX
to this:
XXXXX
Thanks for the examples. I also need to remove any instances of "-", "-X', "-XXXX", etc in the databases so the zip codes just contain five digits. Can someone include an example of this?
Given that your field is named zip , then just do this:
SELECT SUBSTRING_INDEX(zip, '-', 1) as zip
that would return what you want.
To update the data on the table you can do:
update table set zip = SUBSTRING_INDEX(zip, '-', 1) where condition = foo
that would update only records matching some condition, if you want to update them all remove the where part
Instead of using a regex, you could use MySQL's SUBSTRING_INDEX() method:
SELECT SUBSTRING_INDEX(zip_code, '-', 1) FROM your_table
EDIT (to support updates)
UPDATE your_table SET zip_code = SUBSTRING_INDEX(zip_code, '-', 1);
I'd recommend creating a second column, maybe zip_code_short and running SET zip_code_short = instead of overwriting the main data - just to make sure it doesn't cause any errors first (if feasible).

Replace value within a comma-delimited string in MySQL?

Suppose I have the following comma-delimited column value in MySQL: foo,bar,baz,bar,foo2
What is the best way to replace whatever is in the 4th position (in this case bar) of this string with barAAA (so that we change foo,bar,baz,bar,foo2 to foo,bar,baz,barAAA,foo2)? Note that bar occurs both in position 2 as well as position 4.
I know that I can use SUBSTRING_INDEX() in MySQL to get the value of whatever is in position 4, but have not been able to figure out how to replace the value in position 4 with a new value.
I need to do this without creating a UDF or stored function, via using only the standard string functions in MySQL (http://dev.mysql.com/doc/refman/5.5/en/string-functions.html).
Hmm... maybe this?
SELECT #before := CONCAT(SUBSTRING_INDEX(`columnname`,',',3),','),
#len := LENGTH(SUBSTRING_INDEX(`columnname`,',',4)+1
FROM `tablename` WHERE ...;
SELECT CONCAT(#before,'newstring',SUBSTRING(`columnname`,#len+1)) AS `result`
FROM `tablename` WHERE ...;
Replace things as needed, but that should just about do it.
EDIT: Merged into one query:
SELECT
CONCAT(
SUBSTRING_INDEX(`columnname`,',',3),
',newstring,',
SUBSTRING(`columnname`, LENGTH(SUBSTRING_INDEX(`columnname`,',',4)+1))
) as `result`
FROM `tablename` WHERE ...;
That +1 may need to be +2, I'm not sure, but that should work.
You first split your problem in two parts:
locate the comma and split the string in values separated by comma.
update the table with same string and some substring appended.
For the first part I would suggest you take a look here
And for the second part you should take a look here
One more thing there is no shortcut to any problem. You should not run from the problem. Take it as a challenge. Learn while you search for the answer. Best thing take guidance from here and Try to do more researching and efforts.
Try this:
UPDATE yourtable
SET
categories =
TRIM(BOTH ',' FROM
REPLACE(
REPLACE(CONCAT(',',REPLACE(col, ',', ',,'), ','),',2,', ''), ',,', ',')
)
WHERE
FIND_IN_SET('2', categories)
taken from here The best way to remove value from SET field?

How do I query for fields containing a given text in MySQL?

SELECT stuff REGEXP 'itunes' as is_itunes;
In this MySQL query, if "stuff" has the word "itunes" in it, it will mark it as itunes.
However, I want to say "begin with". How can I check for "begin with" instead of anywhere in the text?
SELECT ... WHERE stuff LIKE 'itunes%';
Here % serves as a wildcard character, so this would match rows with the stuff field equal to any of itunes, itunesfoo, itunes1, ...
More info: SQL LIKE Operator at W3Schools.
Add the caret symbol, it represents the beginning of the string:
SELECT stuff REGEXP '^itunes' as is_itunes;
However, LIKE 'itunes%' as suggested by Marc should be a lot faster, especially if your indexes are set up correctly.
SELECT ... WHERE LEFT(stuff, 6) = 'itunes';
For an indexed field it is much better to do:
select ... where stuff >='itunes' AND stuff < 'itunet'
This will not create a full table scan, and use the index.
SELECT ... WHERE stuff REGEXP '[[:<:]]itunes';