1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Returning part of a text based on position of a word?

Discussion in 'Databases' started by mahmood, Nov 4, 2007.

  1. #1
    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
    "
     
    mahmood, Nov 4, 2007 IP
  2. Kuldeep1952

    Kuldeep1952 Active Member

    Messages:
    290
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Try this SQL:

    SELECT 
    	SUBSTRING( myTextField, INSTR(myTextField, 'myWord' )+1, 50) 
    FROM myTable
    Code (markup):
     
    Kuldeep1952, Nov 4, 2007 IP
    mahmood likes this.
  3. mahmood

    mahmood Guest

    Messages:
    1,228
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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?
     
    mahmood, Nov 4, 2007 IP
  4. Kuldeep1952

    Kuldeep1952 Active Member

    Messages:
    290
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #4
    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):
     
    Kuldeep1952, Nov 4, 2007 IP