MySQL custom function example

From , 4 Years ago, written in SQL, viewed 51 times.
URL https://pastebin.vip/view/815104ed
  1. mysql>
  2. mysql> delimiter $$
  3. mysql>
  4. mysql> CREATE FUNCTION myFunction
  5.     ->    (in_string      VARCHAR(255),
  6.     ->     in_find_str    VARCHAR(20),
  7.     ->     in_repl_str    VARCHAR(20))
  8.     ->
  9.     ->   RETURNS VARCHAR(255)
  10.     -> BEGIN
  11.     ->   DECLARE l_new_string VARCHAR(255);
  12.     ->   DECLARE l_find_pos   INT;
  13.     ->
  14.     ->   SET l_find_pos=INSTR(in_string,in_find_str);
  15.     ->
  16.     ->   IF (l_find_pos>0) THEN
  17.     ->     SET l_new_string=INSERT(in_string,l_find_pos,LENGTH(in_find_str),in_repl_str);
  18.     ->   ELSE
  19.     ->     SET l_new_string=in_string;
  20.     ->   END IF;
  21.     ->   RETURN(l_new_string);
  22.     ->
  23.     -> END$$
  24. Query OK, 0 ROWS affected (0.00 sec)
  25.  
  26. mysql>
  27. mysql> delimiter ;
  28. mysql> SELECT myFunction('ABC','A','Z');
  29. +---------------------------+
  30. | myFunction('ABC','A','Z') |
  31. +---------------------------+
  32. | ZBC                       |
  33. +---------------------------+
  34. 1 ROW IN SET (0.00 sec)
  35.  
  36. mysql> DROP FUNCTION myFunction;
  37. Query OK, 0 ROWS affected (0.00 sec)
  38.  
  39. mysql>
  40. mysql>
  41. //SQL/1234

Reply to "MySQL custom function example"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website