MySQL replace numbers after hyphen - mysql

I have a large table with ZIP codes but they are in long format like : 06608-1405
What I require is to REMOVE the hyphen and the numbers after the hyphen. I have using MYSQL REPLACE before but not sure how if you can use Wildcards.
Cheers for you time.
J

How about using SUBSTRING_INDEX:
select SUBSTRING_INDEX(ZIP, '-', 1);
Edit:
To update the table:
Update <table-name> set ZIP = SUBSTRING_INDEX(ZIP, '-', 1);

If the numbers before the hypen are all the same length (5), you can do SUBSTRING(zip, 0, 5)
Or even SUBSTRING(zip, 0, LOCATE('-', zip))
Or LEFT(zip, LOCATE('-', zip)) if you wanna follow the other suggestion that was made.

If they're all ZIP codes (i.e., 5 digits, hyphen, 4 digits), you can just use:
mysql> SELECT LEFT(table.zip_code, 5); // (06608-1405 -> 06608)
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_left

use a combination of the left() the instr() functions
http://dev.mysql.com/doc/refman/5.1/en/string-functions.html
i.e.
select left('12345-6789', instr('12345-6789', '-') - 1)
returns
12345
(could/should be refined to only carry out left() operation if instr() returns a non-zero value).
EDIT: to remove the hyphen etc. use:
update my_table
set my_col = left(my_col, instr(my_col, '-') - 1)

Related

MS SQL Query to remove prefixes

I have below like column values and would like to exclude the characters as well as the hyphen and only return digits. The replace function is not entirely helpful as sometimes the character length is 3 and sometimes its 4, see below as the digit length changes as well.
abc-1234567
sdfr-9876540
try-12345678
case-098765
If you want the part after the last hyphen, you can use substring_index():
select substring_index(col, '-', -1)
You can also extract the digits at the end using regexp_substr():
select regexp_substr(col, '[0-9]+$')

SUBSTR() in MYSQl

How to remove characters from end from any column in MYSQl using SUBSTR ?
For example:
Suppose column value is 1221213.2, so I want to achieve 1221213.
SELECT SUBSTR(colname,1,-2) FROM tablename.
It's this query (assuming your 'remove' is update data in DB):
UPDATE tablename SET colname=SUBSTR(colname, 1, CHAR_LENGTH(colname)-1)
-this will remove 1 symbol from end of string. (so, SUBSTR(colname, 1, CHAR_LENGTH(colname)-2) for 2 sumbols - you've updated)
(upd. there are suggestions for LEFT() function, it's better. I will not repeat that code, however).
It work for firebirb:
select substr(p.name, 1, strlen(rtrim(p.name))-2) from products p
I think for MySQL strlen need replaced with CHAR_LENGTH() function.
You can also use LEFT function.
select LEFT(colname, LENGTH(colname)-2);
LEFT(str, len) selects number of characters from left of the string specified.
SELECT LEFT(colname, CHARACTER_LENGTH(colname)-2);
Prefer CHARACTER_LENGTH() over LENGTH() to avoid surprises.
Now it looks like you are trying to truncate the decimal digits from a decimal number. This is what you might be looking for instead:
SELECT TRUNCATE(colname, 0);
And to update your table:
UPDATE tablename SET colname = TRUNCATE(colname, 0); -- or LEFT(...), or whatever approach you choose
The previous answers all assume that you always want to remove the last 2 characters. But what if there's more (or less) significant digits after the period?
UPDATE your_table
SET colname = SubStr(colname, 0, Locate('.', colname))
WHERE Locate('.', colname) > 0
UPDATE
Even better method:
UPDATE your_table
SET colname = SubString_Index(colname , '.', 1)
WHERE Locate('.', colname) > 0

query to remove all characters after last comma in string

i have a mysql table with this sort of data
TACOMA, Washington, 98477
Now i have thousands of such rows. I want the data to be manipulated in such a manner that it appears like:
TACOMA, Washington
Is it possible though mysql or do i have to manually do it.
You can use :
SELECT SUBSTRING_INDEX('TACOMA, Washington, 98477', ',', 2)
You can read more here.
And the update statement :
UPDATE my_table
SET my_col = SUBSTRING_INDEX(my_col, ',', 2)
Where you need to replace my_table with your table name and my_col with the column you need to be updated.
Possibly this way. Count the number of commas (by checking the length against the length with all the commas removed) and then use SUBSTRING_INDEX to get the string up to the number of commas:-
SELECT SUBSTRING_INDEX(col, ',', LENGTH(col) - LENGTH(REPLACE(col, ',', '')))
FROM SomeTable
substring_index(col, ',',-1)
will give the string from last index of comma to end of string
replace(col,concat(',',substring_index(col, ',',-1)),'')

mysql remove last characters is character if its number

From select statement, in a filed I want to remove last characters is character if its number. Is there string function available in MySQL?
for these two SQL I want
test
as output
select 'test1';
select 'test';
Another way is to use REGEXP,
SET #val = 'test12';
SELECT CONCAT(LEFT(#val, CHAR_LENGTH(#val) - 1),
IF(RIGHT(#val, 1) REGEXP '[0-9]' = 0, RIGHT(#val, 1), ''))
SQLFiddle Demo
SQLFiddle Demo (another example)
To remove the last character if it's numeric, one way to do this without using a regular expression is with LEFT, RIGHT and LENGTH :
select if( right(yourfield,1) = 0 && right(yourfield,1) != '0',
yourfield,
left(yourfield, length(yourfield) - 1))
from yourtable;
To replace all trailing numeric values, you can use REVERSE:
select if( cast(reverse(yourfield) as signed) = 0 && right(yourfield,1) != '0',
yourfield,
left(yourfield, length(yourfield) - length((reverse(yourfield) + 0))))
from yourtable;
SQL Fiddle Demo
When casting fields as integers/signed in MySQL, it will cast all the numeric characters up to the first non-numeric character -- thus making the REVERSE work. If the last character is not numeric, it results in 0.
Using the IF check above, if the last character isn't numeric, then it prints the original value, else it prints all but the last character.
here is a pointer:
use a union between two queries.
in the first - either use REGEX, or grab the substr of the field where another substr for the last char is a number,
then union the text field where the substr of the last char is not a number.
You might want to use Regular Expressions inside MySQL. This package might help you https://launchpad.net/mysql-udf-regexp. However, I do not recommend to do it inside MySQL statement as it might be slow. You would better to do it after grabbing the value inside your programming language.

How do you select rows from a mysql DB where a column begins with a number?

Beginning with a letter I know how to do:
WHERE mov_title REGEXP CONCAT('^(the )?', '$letter')
And this method will work if I substitute $letter with any number, so if its set to 1, it will find all records that begin with 1, but I need it to work for any number 0-9. How could I modify the query?
WHERE mov_title REGEXP '^(the )?[0-9]'
(Or set $letter to [0-9] if you want to keep using your existing WHERE clause.)
Another option may be to use the substring function
WHERE substring( post_title, 1, 1 ) between '0' and '9'
Perhaps SQL Wild cards [charlist] might help:
http://w3schools.com/sql/sql_wildcards.asp