I have a text field and I need a sql query that returns only a part of text. This part should start from the position of a specific word to 50 letters after it. The seuducode would be something like this: SELECT PART of `myTextField` FROM myTable WHERE START IS THE POSITION OF 'myWord' AND END IS 50 LETTERS AFTER THE POSITION OF 'myWord' In real world search engines do that. They only show part of a web page that includes the word searched. Example: lets say I search the word glourious in the following text: "Now is the winter of our discontent Made glorious summer by this sun of York; And all the clouds that lour'd upon our house In the deep bosom of the ocean buried. Now are our brows bound with victorious wreaths;" And it should return: "glorious summer by this sun of York; And all the clouds t"
Try this SQL: SELECT SUBSTRING( myTextField, INSTR(myTextField, 'myWord' )+1, 50) FROM myTable Code (markup):
Worked great. Thanks. Is there a way that we search from the end of the field rather than the begining so we find the last occurance of 'myWord'? And one more question, how do I get other fields now?
To search from the end of the field, you can take the value of the full field, and do it in PHP with function strrchr You can get other fields this way: SELECT SUBSTRING( myTextField, INSTR(myTextField, 'myWord' )+1, 50), Field1, Field2, Field3 FROM myTable Code (markup):