1
DROP FUNCTION IF EXISTS shorten;
delimiter $$
CREATE FUNCTION shorten(s VARCHAR(255), n INT)
 RETURNS VARCHAR(255)
 BEGIN
 IF ISNULL(s) THEN
 RETURN '';
 ELSE IF n<15 THEN
 RETURN LEFT(s, n);
 ELSE IF CHAR_LENGTH(s) <= n THEN
 RETURN s;
 ELSE
 RETURN CONCAT(LEFT(s, n-10), ' ... ', RIGHT(s, 5));
 END IF;
 END$$

The message that I get is:

#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 near '' at line 14 >

Where is teh error..cause I am new to making function in mysql.

UPDATE :

It points to an error with some rounded question mark between those lines:

ELSE IF n <15 THEN RETURN LEFT( s, n ) ;
ELSE IF CHAR_LENGTH( s ) <= n THEN RETURN s;

BTW, I use phpmyadmin

mysql version is: mysqlnd 5.0.10

asked Aug 24, 2012 at 15:21
6
  • Have you tried evaluating a shorter version of this to see if you can narrow down where the problem is? There doesn't seem to be a > in this version. Commented Aug 24, 2012 at 15:23
  • I think the problem might be that you are calling delimiter $$ and then continuing to use ; as a delimiter. Commented Aug 24, 2012 at 15:25
  • 1
    @murgatroid99 this is exactly the way to do it, when declaring a procedure, function or trigger. Commented Aug 24, 2012 at 15:26
  • @murgatroid99 No, that is correct when inserting functions. See: dev.mysql.com/doc/refman/5.0/en/create-procedure.html Commented Aug 24, 2012 at 15:27
  • OK. It's been a while since I've done SQL, so I don't really remember Commented Aug 24, 2012 at 15:27

4 Answers 4

3

There is an extra END IF you need to remove, and replace ELSE IF by ELSEIF:

DROP FUNCTION IF EXISTS shorten;
delimiter $$
CREATE FUNCTION shorten(s VARCHAR(255), n INT) RETURNS VARCHAR(255)
BEGIN
 IF ISNULL(s) THEN
 RETURN '';
 ELSEIF n<15 THEN
 RETURN LEFT(s, n);
 ELSEIF CHAR_LENGTH(s) <= n THEN
 RETURN s;
 ELSE
 RETURN CONCAT(LEFT(s, n-10), ' ... ', RIGHT(s, 5));
 END IF;
END$$

The last error Dmitry had was because of an extra semi-colon:

WRONG code: delimiter $$; 
GOOD code: delimiter $$
answered Aug 24, 2012 at 15:25
Sign up to request clarification or add additional context in comments.

6 Comments

Now I get this with your code: #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 near '' at line 13
yes, the error reported is on the first ELSE IF. I'll fix that
see update.. still get an 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 near '' at line 12
I updated the code, check again please! It works fine using phpmyadmin, I checked it.
#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 near '' at line 12. see update
|
1

I am not sure bt i don't understand why you close if (END IF;) two times..? This may be a probable problem in your code. Please check it.And ELSE IF should be without space i.e ELSEIF. The code should be:

DROP FUNCTION IF EXISTS shorten;
delimiter $$
CREATE FUNCTION shorten(s VARCHAR(255), n INT)
RETURNS VARCHAR(255)
 BEGIN
 IF ISNULL(s) THEN
 RETURN '';
 ELSEIF n<15 THEN
RETURN LEFT(s, n);
 ELSEIF CHAR_LENGTH(s) <= n THEN
 RETURN s;
 ELSE
 RETURN CONCAT(LEFT(s, n-10), ' ... ', RIGHT(s, 5));
 END IF;
 END;$$
 delimiter;
answered Aug 24, 2012 at 15:29

5 Comments

#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 near '' at line 13
the delimiter was not closed properly. that may be a reason. I have updated the code. Could you please check it now.And let me know if it works
Your SQL query has been executed successfully ( Query took 0.0003 sec ).. However I do have some error concerning the delimiter
Why wasnt the delimiter closed properly.. why do you have to close it like this. delimiter;?
The default delimiter in SQL is ';' symbol that defines end of the query. When we define a delimiter, it takes the code between the delimiters as single query. The last line ie 'delimiter;' tell that the delimiter end here. It is done as we do for any other loop like when we use if we end the code as 'END IF;'. you can read more about delimiters at this link buysql.com/mysql/…
1

The real MySQL syntax for IF THEN ELSE is

 IF THEN
 ELSEIF THEN
 ELSE
 END IF

Actually, you're using ELSE IF instead, replace it by ELSEIF, and it's going to work

Reference: http://dev.mysql.com/doc/refman/5.0/en/if.html

DROP FUNCTION IF EXISTS shorten;
delimiter $$
CREATE FUNCTION shorten(s VARCHAR(255), n INT)
RETURNS VARCHAR(255)
 BEGIN
 IF ISNULL(s) THEN
 RETURN '';
 ELSEIF n<15 THEN
RETURN LEFT(s, n);
 ELSEIF CHAR_LENGTH(s) <= n THEN
 RETURN s;
 ELSE
 RETURN CONCAT(LEFT(s, n-10), ' ... ', RIGHT(s, 5));
 END IF;
 END$$
answered Aug 24, 2012 at 15:28

2 Comments

#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 near '' at line 14
This works on my machine. Running SELECT shorten("This is a test",3) I get the expected output: Thi
1
CREATE FUNCTION shorten(s VARCHAR(255), n INT)
RETURNS VARCHAR(255)
BEGIN
IF ISNULL(s) THEN 
RETURN '';
ELSEIF n<15 THEN
RETURN LEFT(s,n);
ELSE 
IF CHAR_LENGTH(s) <= n THEN
RETURN s;
ELSE
RETURN CONCAT(LEFT(s, n-10), '... ',RIGHT(s,5));
END IF;
END IF;
END$$ 

try including one more END IF in ur statement and this should work :)

answered Jun 3, 2015 at 11:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.