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.
Related
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]+$')
I have a column STR which may contain any strings. I'm using MySql. How to find strings which don't contain letters in SQL without using Regular Expressions? As I understand RegExp in SQL is [^...].
So how to select the strings without using [^...]?
Regexp is the most sensible way of doing this. An alternative without...
SELECT STR
FROM YourTable
WHERE NOT EXISTS (SELECT *
FROM (SELECT 'A' AS C
UNION ALL
SELECT 'B'
UNION ALL
SELECT 'C'
/* Todo. Add remaining letters */
) Chars
WHERE INSTR(STR, C) > 0)
I am not sure which RDBMS you're using. But, if you do not want to use regular expression, you can loop through every character in the string and check the ASCII code. If they are only falling in the range 48 to 57, they are only numbers.
Note : This may be very costly operation
How do I remove all superfluous full-stop . and semi-colon ; characters from end of last name field values in SQL?
One way to check of the last character is a "full stop" or "semicolon" is to use a substring function to get the last character, and compare that to the characters you are looking for. (There are several ways to do this, for example, using LIKE or REGEXP operator.
If that last character matches, then lop off that last character. One way to do that is to use a substring function. (Use the CHAR_LENGTH function to return the number of characters in the string.)
For example, something like this:
UPDATE mytable t
SET t.last_name = SUBSTR(t.last_name,1,CHAR_LENGTH(t.last_name)-1)
WHERE SUBSTRING(t.last_name,CHAR_LENGTH(t.last_name),1) IN ('.',';')
But, I'd strongly recommend that you test those expressions using a SELECT statement, before running an UPDATE statement.
SELECT t.last_name AS old_val
, SUBSTR(t.last_name,1,CHAR_LENGTH(t.last_name)-1) AS new_val
FROM mytable t
WHERE SUBSTRING(t.last_name,CHAR_LENGTH(t.last_name),1) IN ('.',';')
Substring rows that have a semi-colon or dot :
update emp
set ename = substring(ename, 1, char_length(ename) - 1)
where ename REGEXP '[.;]$';
I want to truncate the first letter of a string which have size more than 11 character.
I know I can use substring function like
SELECT SUBSTRING(name, 1, 10) from table1;
which will truncate and return me the first 10 letter. what should I do if I want to remove the character from the beginning if the string is greater than 10 character.
abcdefghijklmn ==> efghijklmn
How about RIGHT():
SELECT RIGHT(name, 10)
FROM table1;
Demo: SQL Fiddle
RIGHT() returns a specified number of characters from the right side of a string.
If you want to apply any function only in certain situations, a CASE statement can be used.
create table table1(name char(25));
insert into table1 values('abcdefghijklmn');
select right(name,10) from table1;
RIGHT() is the function you need to use.
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