I would like to delete last character from words from one column in MYSQL and directly add two new to the same word. For example I would like to change beiðni to beiðn|i.
Here is my query = UPDATEds_1_headword_backup1SETstem=
CONCATE((SUBSTRING(stem,1, ,LENGTH(stem) - 2), '|i') WHEREstem
LIKE '%i' ANDgram_1_word_group= 'f' ANDgram_2_endings=
'indecl'
Try something like this:
UPDATE ds_1_headword_backup1 SET stem = concat(substring(stem,1,length(stem)-1), '|i');
Example: SQLFiddle
Here is complete sql query:
UPDATE ds_1_headword_backup1 SET stem = CONCAT(SUBSTRING(stem,1,CHAR_LENGTH(stem) - 1), '|i'), gram_2_endings = '(-i)' WHERE stem LIKE '%i' AND gram_1_word_group = 'f' AND gram_2_endings = 'indecl'
The difference is in using char_length
I agree with chejnik, but don't have enougth reputation to comment his variant, so create another one.
You should use CHAR_LENGTH instead of LENGTH, because CHAR_LENGTH returns count of symbols, but LENGTH - sum of bytes.
SQLFiddle
So, 7alhashmi answer is correct only for strings, which have 1 byte per character.
Related
I was looking for a way to exclude values with a '_' in the results set from a mysql database.
Why would the following sql statement return no results?
select questionKey
from labels
where set_id = 674
and questionKey like 'Class%'
and questionKey not like '%_%' ;
which was the first sql I tried where as
select questionKey
from labels
where set_id = 674
and questionKey like 'Class%'
and locate('_',questionKey) = 0 ;
returns
questionKey
ClassA
ClassB
ClassC
ClassD
ClassE
ClassF
ClassG
ClassNPS
ClassDis
which is the result I wanted. Both SQL statements appear to me to be logically equivalent though they are not.
As tadman and PM77 already pointed out, it's a special character. If you want to use the first query, try to escape it like this (note the backslash):
select questionKey
from labels
where set_id = 674
and questionKey like 'Class%'
and questionKey not like '%\_%' ;
In the LIKE context _ takes on special meaning and represents any single character. It's the only one other than % that means something here.
Your LOCATE() version is probably the best here, though it's worth noting that doing table scans like this can get cripplingly slow on large amounts of data. If underscore represents something important you might want to have a flag field you can set and index.
You could also use a regular expression to try and match records with a single condition:
REGEXP '^Class[^_]+'
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 have a quite big table in mysql and I need to change all the records related to this column.
records are like this :
/name/nm0000209/?ref_=ttfc_fc_cl_t1,
/name/nm0000151/?ref_=ttfc_fc_cl_t2,
...,
/name/nm0104594/?ref_=ttfc_fc_cl_t10
what I want is to keep only the string in the middle which is nm0000209, nm0000151,.... I know how to delete specific characters from the right or left of the words by REPLACE or Trim , .., but my problem is that in this case the number of characters in the third part of string are not equal (as you see when it reaches to 10, I have to delete 21 characters from the end instead of 20 characters and since this table contains lots of records I dont know how to do it.
I reaaly appreciate if someone could helop me,
thanks
I want is to keep only the string in the middle which is nm0000209, nm0000151...
You can use 'SUBSTRING_INDEX' on the column to crop part of the column value.
Following example assumes that the said column will have 'name/' as starting pattern.
Example:
update table_name
set column_name = substring_index(
substring_index( column_name, 'name/', -1 )
, '/', 1 );
The same can be used for updating with the same value.
Demo # MySQL Fiddle
One approach would be to use MYSQL's SUBSTRING_INDEX function. It would let you get whatever's after the last slash. Or after the second to last.
For your particular case
select
SUBSTRING_INDEX(SUBSTRING_INDEX(thefield,'/',-2 ),'/', 1)
from supertext
would yield the desired result
EDIT: for update purposes
UPDATE thetable
SET thefield=SUBSTRING_INDEX(SUBSTRING_INDEX(thefield,'/',-2 ),'/', 1)
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
I have a table of users which has a username column consisting of a six digit number e.g 675381, I need to prepend a zero to each of these usernames e.g. 0675381 would be the final output of the previous example, is there a query that could handle this?
UPDATE Tablename SET Username = Concat('0', Username);
what type is the column of?
if it's string type, try something like this:
UPDATE your_table SET column_name=concat('0',column_name);
You mean "prepend" ? i.e. add it on the front?
Is the column numeric? Do you always want 7 characters output?
Assuming that, something like this would work for a query:
select LPAD(CONVERT(username, CHAR), 7, '0')
If the column is characters, the CONVERT() part is unnecessary, just LPAD the username.
If you want to permanently modify the value in the table, you'll need to ensure the column is a character type and UPDATE using the above.
You might want to use CONCAT_WS('', '0', Username) because if there is a null value, then you'll end up with NULL instead of '0'. This probably isn't a problem, but something I've learnt the hard way.