1

I successfully created the following function in mysql

CREATE FUNCTION h2m
HTMLText(text)
RETURNS text
DETERMINISTIC
BEGIN
DECLARE Start INT;
DECLARE End INT;
DECLARE Length INT;
SET Start = CHARINDEX('<',HTMLText);
SET End = CHARINDEX('>',HTMLText,CHARINDEX('<',HTMLText));
SET Length = (End - Start) + 1;
WHILE Start > 0
AND End > 0
AND Length > 0
DO
SET HTMLText = STUFF(HTMLText,Start,Length,'');
SET Start = CHARINDEX('<',HTMLText);
SET End = CHARINDEX('>',HTMLText,CHARINDEX('<',HTMLText));
SET Length = (End - Start) + 1;
END WHILE;
RETURN LTRIM(RTRIM(HTMLText));
END;

Now when I'm trying to call it with the following code:

SELECT description h2m (HTMLText) FROM oc_product_description,

I am getting the following error:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use in '(HTML Text) FROM oc product_description LIMIT 0, 30' at line 2 –

Thank you so much for your help

2 Answers 2

1

You have two problems. First the CHARINDEX() function isn't a built-in MySQL function. Unless you have it custom defined somewhere in your DB, you'll run into an error here. LOCATE() is the built-in MySQL function that does something similar to what CHARINDEX() does.

Your second problem is the way you're calling the function. You should be calling it in this fashion

SELECT h2m (description) 
FROM oc_product_description
Sign up to request clarification or add additional context in comments.

3 Comments

Everything you mentioned worked like a charm, Would you please give me a command to execute the function that I have stored? the command I have right now only selects them. This function is suppose to search description column and convert html codes to text by eliminating the html tags.
@Schwann I am not sure I understand your question. The command you have right now is already searching and converting your HTML
@Liyod, I did use that command, but it did not update and change the column, it just selected and gave me a list.
1

You can try:

SELECT description, h2m(HTMLText)
FROM oc_product_description

Three things:

  • Comma after description.
  • Removing space between h2m and (. MySQL has a problem with spaces after function names.
  • Removal of comma after oc_product_description.

EDIT:

Perhaps you want to pass description in as the argument:

SELECT h2m(description)
FROM oc_product_description

2 Comments

Thank you @Gordon Linoff, I ran yours and It gives me unknown column HTMLText error
Now it says, Commands out of sync, you cannot run this command now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.