0

For the user-define function in MYSQL,

I passs in one parameter to udf (a int);

if a is NULL return 2.0; else return 3.0

`DELIMITER !
CREATE FUNCTION return val(a INTEGER) RETURNS NUMERIC(10,2)
BEGIN
  If (isNULL(a)) THEN return 2.0
  ELSE RETURN 3.0;
  END IF;
END!

DELIMITER ;`

Is this the right way to do it?

6
  • Yes, apparently, there isn't a function call isNull, so how can I tell about whether the a is a NULL Commented Nov 2, 2016 at 0:14
  • So do I change it to if (SELECT a IS NULL) THEN RETURN 2.0 Commented Nov 2, 2016 at 0:31
  • how about replacing isNull by ifNull ? Commented Nov 2, 2016 at 2:25
  • @RyanVincent, thanks for your input Commented Nov 2, 2016 at 2:39
  • @bogzy, you mean IF ( IFNULL(a, 0)) THEN .... ? Commented Nov 2, 2016 at 2:39

1 Answer 1

1

You may also try this one using CASE statement:

CASE WHEN a IS NULL or a = ''
     then '2.0'
     else '3.0'
END 
Sign up to request clarification or add additional context in comments.

Comments

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.