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).
Related
I am moving data from Oracle to MySQL, this data is relevant for several views.
In one View, part of a string needs to be stripped from the result in one field of the view - if it exists.
So 643726493.234 would be fine, but 643726493.234-tzuv would need to turn into 643726493.234.
(How) can I do that?
If the string have always the - charachter separating the two part you can use somethings like this
select substr(your_column, 1, POSITION('#' IN your_column)-1) from your_table;
or for update
update your_table
set your_column = select substr(your_column, 1, POSITION('#' IN your_column)-1);
In my database I have a table called 'content' and a field called 'link' and there are almost 300,000 records in that table.
In the field called 'link' there are a number of records that look like this :
http://www.example.com/blah/blah/123456789/url=http://www.destination.com
Unfortunately the prefix part of the records are individually unique where the numbered portion is constant changing from 90 to 150 alpha-numeric characters
I would like to remove the prefix up to and/or including the url=
So that the only thing left in the record is :
http://www.destination.com OR
I could even work with
url=http://www.destination.com
and simply do a replace command against the "url=" part as a second mysql command.
If there was a wildcard command, this job would be much easier and I would just wildcard everything showing up in the link record between :
http://www.example.com/blah/blah/ wildcard url=
But as everyone knows... there is no such wildcard available
So it had me looking at the UPDATE, SET and TRIM(LEADING commands
UPDATE content
SET link =
TRIM(LEADING 'url=' FROM link)
But this DID NOT generate the changes I wanted
And so I took the labor intensive method of downloading the database and using a Search and Replace program to make the changes to the 44 thousand records that contained these parameters.
But I would love to find a command that I could simply pass to the database to make this simpler in the future.
Any thoughts on how to accomplish this change in the future would be greatly appreciated.
Thanks in advance ;
You can use the SUBSTRING_INDEX function:
UPDATE content SET link=SUBSTRING_INDEX( `link` , 'url=', -1 )
I have not tested it, so I would recommend you check that substring_index returns the desired string first.
Assuming that the part you want to keep always begins with 'http://' you could get the desired result string with the help of the SUBSTRING_INDEX function:
SELECT CONCAT('http://', SUBSTRING_INDEX(link, 'http://', -1)) FROM content;
and fix your table with the simple statement
UPDATE
content
SET
link = CONCAT('http://', SUBSTRING_INDEX(link, 'http://', -1));
Explanation:
SUBSTRING_INDEX with third parameter negative returns the substring from the last occurence of the needle in the second parameter to the end. Because 'http://' isn't included in the return value, we add it again.
Remark:
If you've got https:// urls too, you should be able to adapt my solution.
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.
I am struggling with this query and want to know if I am wasting my time and need to write a php script or is something like the following actually possible?
UPDATE my_table
SET #userid = user_id
AND SET filename('http://pathto/newfilename_'#userid'.jpg')
FROM my_table
WHERE filename
LIKE '%_%' AND filename
LIKE '%jpg'AND filename
NOT LIKE 'http%';
Basically I have 700 odd files that need renaming in the database as they do not match the filenames as I am changing system, they are called in the database.
The format is 2_gfhgfhf.jpg which translates to userid_randomjumble.jpg
But not all files in the database are in this format only about 700 out of thousands. So I want to identify names that contain _ but don't contain http (thats the correct format that I don't want to touch).
I can do that fine but now comes the tricky bit!!
I want to replace that file name userid_randomjumble.jpg with http://pathto/filename_userid.jpg So I want to set the column user_id in that row to a variable and insert it into my new filename.
The above doesn't work for obvious reasons but I am not sure if there is a way round what I'm trying to do. I have no idea if it's possible? Am I wasting my time with this and should I turn to PHP with mysql and stop being lazy? Or is there a way to get this to work?
Yes it is possible without the php. Here is a simple example
SET #a:=0;
SELECT * FROM table WHERE field_name = #a;
Yes you can do it using straightforward SQL:
UPDATE my_table
SET filename = CONCAT('http://pathto/newfilename_', userid, '.jpg')
WHERE filename LIKE '%\_%jpg'
AND filename NOT LIKE 'http%';
Notes:
No need for variables. Any columns of rows being updated may be referenced
In mysql, use CONCAT() to add text values together
With LIKE, an underscore (_) has a special meaning - it means "any single character". If you want to match a literal underscore, you must escape it with a backslash (\)
Your two LIKE predicates may be safely merged into one for a simpler query
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'