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
Related
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.
i have field with content like this: 1, 2, 100 first two numbers can be any size third one can be up to 100.
Now i need to sort my fields by this third number but since i have 2 other numbers i don't know how to do it.
Maybe i could use something like REGEXP or something else?
So far i've tried SUBSTRING but since my two numbers can be any lenght something like
order by SUBSTRING(my_field, 4)
would work but if i have numbers like 32, 451, 30 it takes wrong numbers
Also i use php for to build my query, but i don't think it matters.
You can use SUBSTRING_INDEX. So something like:
SUBSTRING_INDEX(my_field, ',', -1)
EDIT: if you have spaces you might want to do some trimming as well.
ORDER BY SUBSTRING_INDEX(my_field,',',-1)+0 ASC -- or DESC
Use SUBSTRING_INDEX, which grabs the substring up to the nth occurence of the delimiter (in your case, comma), or, in the case of a negative n, it will return the substring after the nth occurence from the end.
To see what I mean try:
SELECT SUBSTRING_INDEX(my_field,',',-1)
FROM my_table
The reason there is a +0 in the ORDER BY is so that mysql sorts these as numbers, not strings.
This should work:
order by CONVERT(SUBSTRING(my_field, LOCATE(",", my_field, RIGHT)), INTEGER)
SELECT *
FROM (
SELECT
SUBSTRING(my_field, 4) AS my_field,
...
FROM
...
WHERE
...
) temp_table
ORDER BY my_field
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)
I'd like to select rows from the database where the last character in the mov_id column equals to 1 and 2.
How would the query look like?
SELECT * FROM `myTable` WHERE `mov_id` LIKE '%1' OR `mov_id` LIKE '%2'
the % character is a wildcard which matches anything (like * in many other places)
If mov_id is a numeric value (TINYINT, INT, etc...) then you should use a numeric operator. For instance, use the modulo operator to keep the last digit
SELECT * FROM mytable
WHERE (mov_id MOD 10) IN (1, 2)
If mov_id is a string, you can use LIKE or SUBSTRING(). SUBSTRING() will be slightly faster.
SELECT * FROM mytable
WHERE SUBSTRING(mov_id, -1) IN ('1', '2')
If your table is big or that query is frequently run, you should definitely consider adding a column to your table, in which you would store mov_id's last digit/character, and index that column.
Try this way too:
SELECT field1
FROM table
WHERE RIGHT(field1, 1) = 'x'
it displays the fields that has last a value of x.
SELECT *
FROM Table
WHERE RIGHT(Column_name, 1) IN ('x')
if you want to match two character just replace 1 by 2.
In general:
RIGHT(COLUMN_NAME, NO_OF_CHARACTER_YOU WANT_TO_MATCH_FROM_LAST)
And if you want to match the starting char just use LEFT instead of RIGHT
You could also do something like:
select
your, fields, go, here
from table
where
substring(mov_id, (char_length(move_id) - 1)) = x
SELECT * FROM table WHERE mov_id REGEXP '1$' OR mov_id REGEXP '2$'
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