MySql Trimming characters till a space - mysql

How can I Trim (remove) all characters from the Right of a string upto the first space?
Here's what I'm trying to do:
set #s:='May the Gods watch over your battles, my friend!';
select if(length(#s) < 10,#s,left(#s,10)) a;
#output from above: 'May the Go'
#desired output: 'May the'
To avoid such odd outputs like May the Go I'm trying to trim all characters from the right, upto the first space, so the output is May the.
How can this be done in the sql statement itself. I could not find a built in function that'll do this?

This works in Microsoft SQL, it should work if you replace CHARINDEX with INSTR
select substring(#s,1,charindex(' ',reverse(#s)))
Added a my SQL fiddle version below, works a bit different than in Microsoft SQL
http://sqlfiddle.com/#!2/d41d8/44718
select #s,left(#s,10-locate(' ',reverse(#s)));
Example within the database
select theFld,
CASE
WHEN length(theFld) <= 20 THEN theFld
ELSE
left(theFld,20-locate(' ',reverse(left(theFld,20))))
END as Abbr
FROM example;
See this SQL fiddle : http://sqlfiddle.com/#!2/beac7/6

You can try this way :
.....
.....
--define max length for the extracted text
set #length:=10;
set #result =
--check if either the last extracted character or...
--the character next to the last is a space (or both are spaces)
if(substring(#s, #length, 2) LIKE '% %',
--if true, extract using simple left()'
left(#s,#length),
--else, use the same simple left() operation then remove all characters..
--starting from the right-most until the first space found
replace(left(#s,#length), substring_index(left(#s, #length), ' ', -1), '')
);
[SQL Fiddle demo]
For reference : MySQL String Last Index Of

Related

Mysql: extract a string from field between delimiters (backwards)

I have a Column 'ACCOUNT_NUMBER' from a table 'BankingActivity' which contains data as follow :
example:
ManualBanking-BankDeposit-350-1006590343--INTERNAL_A
or
MyPayCard-MyPayDeposit-620-989228234--TL
I need to extract the number '1006590343' or '989228234'
Initially i execute the following query:
select substr( `BankingActivity`.`ACCOUNT_NUMBER`,(
locate( '--', `BankingActivity`.`ACCOUNT_NUMBER` ) - 9 ),9 ) * 1
from BankingActivity
Which works fine if the length of the string does not exceed 9 digits. Over 9 digits, I obviously have issues and can not get the full string.
How can i look backwards for the delimiter '--' and then extract the value between the '--' delimiter and the previous '-' delimiter?
I tried with some Regex but I am not familiar enough with it to get a correct result.
Try
SELECT regexp_substr(
regexp_substr(acct, '-\\d+--'), '\\d+')
FROM (
SELECT 'ManualBanking-BankDeposit-350-1006590343--INTERNAL_A' as acct
UNION
SELECT 'MyPayCard-MyPayDeposit-620-989228234--TL'
) accounts;
The inner regexp_substr extracts a substring that begins with a dash followed by 1 or more digits and ends with two dashes. That would be e. g. '-1006590343--'. From this, the outer regexp_substr extracts all consecutive digits, that is '1006590343'.
More detailed information about regular expressions in MySQL can be found in the documentation.
If I have understood your question correctly then you can try something like this -
select SUBSTRING_INDEX(SUBSTRING_INDEX('ManualBanking-BankDeposit-350-1006590343--INTERNAL_A', '-' ,-3), '--', 1);
select SUBSTRING_INDEX(SUBSTRING_INDEX('MyPayCard-MyPayDeposit-620-989228234--TL', '-' ,-3), '--', 1);
This is probably a job for SUBSTRING_INDEX().
Check it out. Fiddle here.
SET #s = 'ManualBanking-BankDeposit-350-1006590343--INTERNAL_A';
SELECT SUBSTRING_INDEX(#s, '-', -3);
This splits your string on '-'. It takes everything after the third '-' delimiter from the end, and gives you back 1006590343--INTERNAL_A.
Then we use SUBSTRING_INDEX() again on that.
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(#s, '-', -3), '-', 1);
Lo and behold, this gets us 1006590343.
But. This is a brittle way to do it. MySQL's string processing isn't easy to program in detailed ways. This solution doesn't take into account things like missing dashes at the end of the string. Garbage in, garbage out. Use a host language like C# / php / nodejs / Java etc to do this kind of string analysis if you want it to be super-robust for real world data.

MySQL remove everything after n words in column

I have a table with over 5000 entries, and basically I want to replace the texts with excerpts. So the column 'text' has between 1000 and 2000 words, most of the time. I want every cell to be cut after 80 words. Additionally it would be nice to add something like 'Read more...' after the 80 words. Is it possible with a MySQL Query?
This is bad idea to do that from MySQL level. This is about the VIEW Layer, so should be programmed in the place which read the data from the database and presents them.
In PHP, it can be done with
function cutStringAfterWords($phrase,$max_words){
$phrase_array = explode(' ',$phrase);
if(count($phrase_array) > $max_words && $max_words > 0)
$phrase = implode(' ',array_slice($phrase_array, 0, $max_words)).'...';
return $phrase;
}
echo cutStringAfterWords($largeText,80).' Read more...';
, but I believe most of the languages have their equivalents.
Edit: made an example to cut after 80 words. you can simply replace 3 dots within function to place Read more always, or remove 3 dots in function, and manually add 'Read More...' string after every truncated text
You can try with regex.
select REGEXP_SUBSTR(your_column_name,'(.+?\\s+|.+?){1,80}')
from your_table_name
There's nice MySQL string function SUBSTRING_INDEX which you can use like this to get the result you want:
UPDATE your_table SET your_text_field = (SELECT SUBSTRING_INDEX(your_text_field, ' ', 80) FROM your_table WHERE text_id = some_id) WHERE text_id = some_id;
To add 'Read More..' you can use MySQL CONCAT.
Also, I would suggest backing up your table before you start experimenting.
execute below query to update all records with first 80 characters
update your_table_name set column_name=SUBSTRING_INDEX(column_name, ' ', 80)
Updated with SUBSTRING_INDEX since you need words

Extract specific pattern from string in MySQL:

I would like to extract specific pattern from string in MySQL.
The column contains specific string like xxx-atg168d and xxx-atg444-6x. From these string, I want to extract atg168 and atg444 only. How can I perform this in MySQL?
**Input_column**
xxx-atg168d
xxx-atg444-6x
xxx-atg1689d
xxx-atg16507d
xxx-atg444d-6x
xxx-atg444c-6x
**Output_column**
atg168
atg444
atg1689
atg16507
atg444
atg444
Something like this may meet your specification:
SUBSTRING_INDEX(SUBSTR( t.col ,INSTR( t.col ,'-')+1),'-',1)
This assumes that you want to return the portion of the string following the first dash character, up to the next dash character (if present). If no dash characters exist within the string, the entire string will be returned.
EDIT
Ooops. That expression also includes the trailing "d". If it's just a trailing "d" character that needs to be removed...
TRIM(TRAILING 'd' FROM SUBSTRING_INDEX(SUBSTR( t.col ,INSTR( t.col ,'-')+1),'-',1))
In the more general case, to remove any "non-digit" character from the end (not just a "d"), things get pretty ugly. We need to check the rightmost character, and see if it matches a character we wnt to keep. If it's not, we shorten the string by one character.
IF( INSTR('0123456789',RIGHT(
#t := SUBSTRING_INDEX(SUBSTR( t.col ,INSTR( t.col ,'-')+1),'-',1)
,1))
, #t
, SUBSTRING( #t, 1, CHAR_LENGTH( #t )-1)
)
I made use of a user-defined variable here to avoid repeating the same expression multiple times. It's not required that we do that. The #t :=assignment can be removed, and other occurrences of #t can be replaced with the expression that was assigned to #t.
The literal '0123456789' in that expression is the set of characters that we don't want to remove from the end of the string.
Use SUBSTRING function , like this :
select SUBSTRING(column_name ,5,6) from table_name;
Here 5 is starting position and 6 is length of sub-string getting extracted from string.
Thanks spencer for your suggestions. I edited your code to get the solution for my query. Here is the update query,
left(substring_index (substr(subid,instr(subid,'-')+1),'-',1) , char_length(substring_index (substr(subid,instr(subid,'-')+1),'-',1))-1)

removing characters from field in MS Access database table

Using MS Access 2010.
I have a field in a table that contains windows path names surrounded by quotes, like this
"C:\My Documents\Photos\img1.jpg"
"C:\My Documents\Photos\products\gizmo.jpg"
"C:\My Documents\Photos\img5.jpg"
and so on.
I need to get rid of the quotes so the column looks like this:
C:\My Documents\Photos\img1.jpg
C:\My Documents\Photos\products\gizmo.jpg
C:\My Documents\Photos\img5.jpg
Is there a way to write an update query to do this?
OR a better way to do it altogether?
If you will be doing this from within an Access session, using Access 2000 or later, you can use the Replace() function in an update query to remove the quotes. Remove would mean replace them with an empty string.
UPDATE YourTable
SET path_field = Replace(path_field, '"', '');
If any of those path strings could include quotes within them (yuck!), consider the Mid() function ... ask it to start at the 2nd character (skipping the lead quote), and return the number of characters equivalent to Len(path_field) - 2
UPDATE YourTable
SET path_field = Mid(path_field, 2, Len(path_field) - 2);
Either way, you may want to include a WHERE clause to ignore rows without path_field values.
WHERE Len(path_field) > 0
And if you must do this again when new data is added, use a different WHERE clause to ensure you UPDATE only those rows whose path_field values start and end with quotes.
WHERE path_field Like '"*"'
That was using the * wild card for Access' default ANSI 89 mode. If you will do this from ADO (ANSI 92 mode), use the % wild card.
WHERE path_field Like '"%"'
... or use ALike and the % wild card with either mode.
WHERE path_field ALike '"%"'
The solution with REPLACE already mentioned by others works, but removes ALL quotes, even if they are in the middle of the string.
If you only want to remove quotes at the beginning or at the end, but leave quotes in the middle of the string as they are, you can do it with the following two queries:
Remove first character if it's a quote:
update YourTable
set YourField = right(YourField, len(YourField) - 1)
where left(YourField, 1) = '"'
Remove last character if it's a quote:
update YourTable
set YourTable = left(YourField, len(YourField) - 1)
where right(YourField, 1) = '"'
To make this a permanent change, you might run an update query that looked something like this:
UPDATE [Your Table]
SET [Your Table].[Your Field] = Replace([Your Table].[Your Field],"""","")
This will get rid of all quotes, even if they aren't at the beginning or end. Post back if that's not exactly what you want.
Assuming your column name is MyColumn and table name is MyTable, you can use this sql to update your data to get rid of quotes.
UPDATE MyTable
SET MyColumn = REPLACE(MyColumn,'"','')

Format Phone Numbers in MySQL

I have a bunch of phone numbers in a DB that are formatted as such: (999) 123-3456.
I'm needing them to look like 123-123-1234
Is there any sort of regex or something I can do in MySQL to quickly format all these phone numbers?
Also, frustratingly, some are NOT formatted like that, so I couldn't just apply this to an entire column.
Thanks!
A quick solution would be to run these two queries:
UPDATE table_name set PhoneCol = REPLACE(PhoneCol, '(', '');
UPDATE table_name set PhoneCol = REPLACE(PhoneCol, ') ', '-');
Just write a small php script that loops through all the values and updates them. Making that change is pretty simple in php. Then just run an update on the row to overwrite the value.
maybe a two pass solution.
strip out all non-numeric characters (and spaces)
inset the formatting characters '(',')', ' ', and '-' into the correct spots
(or better yet, leave them off and format only during select on your reports.)
I had a similar problem, but increased by the reason that some phones had the format with the dashes and others did not and this was the command that helped me to update the formats of the numbers that did not have the hyphens.
Phone before the command: 1234567890
Phone after command: 123-456-7890
The phone field is called phone_number and is a VARCHAR
The command I used is:
UPDATE database.table
SET phone_number = concat(SUBSTRING(phone_number,1,3) , '-' , SUBSTRING(phone_number,4,3) , '-' , SUBSTRING(phone_number,7,4))
WHERE LOCATE('-', phone_number) = 0;
I think your command could be like this:
UPDATE database.table
SET phone_number = concat(SUBSTRING(phone_number,2,3) , '-' , SUBSTRING(phone_number,7,8));
I would remove the WHERE clause under the assumption that all phones would be formatted with the (). Also, the second string of characters would start from position 7 because there appears to be a space after the parentheses.