views:

29

answers:

1

I am using TinyMCE to allow users to write in what will eventually be submitted to a database.

TinyMCE includes a button which will insert a "pagebreak" (actually just an HTML comment <!--pagebreak-->).

How can I later pull back everything up to the first occurrence of the pagebreak "tag"? There might be a significant bit of data after that tag which I could discard via php later, but it seems like I shouldn't even bring it back. So I can I SELECT just part of a column if I never know how far in to the data the <!--pagebreak--> will be?

EDIT: The below answer does work, and will get me where I need to go.. I am just curious as to if there is an equally simple way to handle bringing back the whole column if it is short and thus the pagebreak tag doesn't exist. With the below solution, if no pagebreak is found, an empty string is returned.

If not, then I can easily handle it in the code.

+2  A: 

Something like this should get you everything up to the <!--:

SELECT SUBSTRING(my_col FROM 1 FOR POSITION('<!--' IN my_col)-1) AS a_chunk 
  FROM my_table

Reference for string functions.

Edit:
Just putting OMG Ponies' words into SQL:

SELECT CASE WHEN position('<!--' IN my_col) > 0 
            THEN SUBSTRING(my_col FROM 1 FOR POSITION('<!--' IN my_col)-1)
            ELSE my_col END AS a_chunk
  FROM my_table

It sounds like you'll also want a check on the length of the text; whether or not there is a page break. You can use CHARACTER_LENGTH() for that.

Adam Bernier
+1: That's what I was thinking -- just a simple case of substringing, using either POSITION or LOCATE... It's even *formatted* like I'd have written it!
OMG Ponies
Is there a way to handle a short input that may not have a pagebreak?
Chris Sobolewski
That's not what I'm seeing in testing. It appears as though if there is no page break, an empty string is returned.
Chris Sobolewski
@Chris Sobolewski: My bad, POSITION will return zero if the text is not found. So you need a CASE statement to split handling if there's a page break in the column for appropriate substringing.
OMG Ponies