MySQL remove everything after n words in column - mysql

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

Related

MySql Trimming characters till a space

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

mysql to update a database using UPDATE SET and TRIM(LEADING wildcard prefix in record

In my database I have a table called 'content' and a field called 'link' and there are almost 300,000 records in that table.
In the field called 'link' there are a number of records that look like this :
http://www.example.com/blah/blah/123456789/url=http://www.destination.com
Unfortunately the prefix part of the records are individually unique where the numbered portion is constant changing from 90 to 150 alpha-numeric characters
I would like to remove the prefix up to and/or including the url=
So that the only thing left in the record is :
http://www.destination.com OR
I could even work with
url=http://www.destination.com
and simply do a replace command against the "url=" part as a second mysql command.
If there was a wildcard command, this job would be much easier and I would just wildcard everything showing up in the link record between :
http://www.example.com/blah/blah/ wildcard url=
But as everyone knows... there is no such wildcard available
So it had me looking at the UPDATE, SET and TRIM(LEADING commands
UPDATE content
SET link =
TRIM(LEADING 'url=' FROM link)
But this DID NOT generate the changes I wanted
And so I took the labor intensive method of downloading the database and using a Search and Replace program to make the changes to the 44 thousand records that contained these parameters.
But I would love to find a command that I could simply pass to the database to make this simpler in the future.
Any thoughts on how to accomplish this change in the future would be greatly appreciated.
Thanks in advance ;
You can use the SUBSTRING_INDEX function:
UPDATE content SET link=SUBSTRING_INDEX( `link` , 'url=', -1 )
I have not tested it, so I would recommend you check that substring_index returns the desired string first.
Assuming that the part you want to keep always begins with 'http://' you could get the desired result string with the help of the SUBSTRING_INDEX function:
SELECT CONCAT('http://', SUBSTRING_INDEX(link, 'http://', -1)) FROM content;
and fix your table with the simple statement
UPDATE
content
SET
link = CONCAT('http://', SUBSTRING_INDEX(link, 'http://', -1));
Explanation:
SUBSTRING_INDEX with third parameter negative returns the substring from the last occurence of the needle in the second parameter to the end. Because 'http://' isn't included in the return value, we add it again.
Remark:
If you've got https:// urls too, you should be able to adapt my solution.

How to remove -XXXX from Zip Code field using MySQL REGEXP?

Can someone give me an example of how to remove anything after a dash "-" in a zip code field using SQL commands?
For example, change any of this:
XXXXX-X
XXXXX-
XXXXX-XX
XXXXX-XXXX
to this:
XXXXX
Thanks for the examples. I also need to remove any instances of "-", "-X', "-XXXX", etc in the databases so the zip codes just contain five digits. Can someone include an example of this?
Given that your field is named zip , then just do this:
SELECT SUBSTRING_INDEX(zip, '-', 1) as zip
that would return what you want.
To update the data on the table you can do:
update table set zip = SUBSTRING_INDEX(zip, '-', 1) where condition = foo
that would update only records matching some condition, if you want to update them all remove the where part
Instead of using a regex, you could use MySQL's SUBSTRING_INDEX() method:
SELECT SUBSTRING_INDEX(zip_code, '-', 1) FROM your_table
EDIT (to support updates)
UPDATE your_table SET zip_code = SUBSTRING_INDEX(zip_code, '-', 1);
I'd recommend creating a second column, maybe zip_code_short and running SET zip_code_short = instead of overwriting the main data - just to make sure it doesn't cause any errors first (if feasible).

Replace an exact string in mysql that includes square brackets

I am very new to sql querys and I am looking for a way to remove the following string from anywhere in my database: [lesson-navigation] including the square brackets. (Some instances have an unnecessary extra space before and/or after the string that would be nice to delete as well if possible too.
This was a shortcode that I was inserting into my site, but then decided to implement it into the template instead. Now I have over 2000 instances of this in various places in my database and want to remove it.
Thanks in advance for your help.
~Cam
Use REPLACE() to perform a string replacement that removes the string from each column. If this string appears in multiple columns, you'll need to perform the query below for each column, or specify each column in the query (second example):
UPDATE tablename SET columnname = REPLACE(columnname, '[lesson-navigation]', '');
/* Replace in multiple columns */
UPDATE tablename SET
columnname = REPLACE(columnname, '[lesson-navigation]', ''),
columnname2 = REPLACE(columnname2, '[lesson-navigation]', ''),
columnname3 = REPLACE(columnname3, '[lesson-navigation]', '')
;

Extract specific words from text field in mysql

I have a table that contains a text field, there is around 3 to 4 sentences in the field depending on the row.
Now, I am making an auto-complete html object, and I would like to start typing the beginning of a word and that the database return words that start with those letters from the database text field.
Example of a text field: I like fishsticks, fishhat are great too
in my auto-complete if I would type "fish" it would propose "fishsticks" and "fishhat"
Everything works but the query.
I can easily find the rows that contains a specific word but I can't extract only the word, not the full text.
select data_txt from mytable match(data_txt) against('fish', IN BOOLEAN MODE) limit 10
I know it is dirty, but I cannot rearrange the database.
Thank you for your help!
EDIT:
Here's what I got, thanks to Brent Worden, it is not clean but it works:
SELECT DISTINCT
SUBSTRING(data_txt,
LOCATE('great', data_txt),
LOCATE(' ' , data_txt, LOCATE('great', data_txt)) - LOCATE('great', data_txt)
)
FROM mytable WHERE data_txt LIKE '% great%'
LIMIT 10
any idea on how to avoid using the same LOCATE expression over and over?
Use LOCATE to find the occurrence of the word.
Use LOCATE and the previous LOCATE return value to find the occurrence of the first space after the word.
USE SUBSTR and the previous 2 LOCATE return values to extract the whole word.
$tagsql ="SELECT * from mytable";
$tagquery = mysql_query($tagsql);
$tags = array(); //Creates an empty array
while ($tagrow = mysql_fetch_array($tagquery)) {
$tags[] = tagrow['tags']; //Fills the empty array
}
If the rows contain commas you could use -
$comma_separated = implode(",", $tags);
you can replace the comma for spaces if they are separated as spaces in your table.
$exp = explode(",", $comma_separated);
If you require your data to be unique you may include the following:
$uniquetags = array_unique($exp, SORT_REGULAR);
you can use print_r to see the results of the array resulting
Here array_merge is used because $rt will not get displayed if you are using a 'jquery' autocomplete else $rt may work and array_merge can be ignored. However, you may use array_merge to include multiple tables by repeating the previous process.
$autocompletetags = array_merge((array)$uniquetags);
This sorts the values in the alphabetic order
sort($autocompletetags, SORT_REGULAR);