ferseg

dec_a_romanos

Oct 8th, 2015
3,126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.67 KB | None | 0 0
  1. /*
  2.     Autor   : Joel Fernandez
  3.     Fecha   : 08/10/2015
  4.     IDE     : MySQL Workbench
  5.     Web     : www. codebotic.com
  6.     Tema    : FUnciones en MySQL
  7. */
  8.  
  9. -- 4° convertir un numero decimal (maximo 2 cifras) a romanos
  10.  
  11. delimiter *
  12.     create function dec_a_romanos(n int)
  13.     returns varchar(50)
  14.    
  15.     begin
  16.         declare x int;
  17.         declare y int;
  18.         declare rom varchar(50);
  19.        
  20.         set rom='';
  21.        
  22.         set x=(n/10)-(n%10)/10; -- obtenemos la decena
  23.        
  24.         set y=n%10;-- obtenemos las unidades
  25.        
  26.         case(X)
  27.             when '1' then set rom=concat(rom,'X');
  28.             when '2' then set rom=concat(rom,'XX');
  29.             when '3' then set rom=concat(rom,'XXX');
  30.             when '4' then set rom=concat(rom,'XL');
  31.             when '5' then set rom=concat(rom,'L');
  32.             when '6' then set rom=concat(rom,'LX');
  33.             when '7' then set rom=concat(rom,'LXX');
  34.             when '8' then set rom=concat(rom,'LXXX');
  35.             when '9' then set rom=concat(rom,'XC');
  36.            
  37.         end case;
  38.        
  39.         case(Y)
  40.             when '1' then set rom=concat(rom,'I');
  41.             when '2' then set rom=concat(rom,'II');
  42.             when '3' then set rom=concat(rom,'III');
  43.             when '4' then set rom=concat(rom,'IV');
  44.             when '5' then set rom=concat(rom,'V');
  45.             when '6' then set rom=concat(rom,'VI');
  46.             when '7' then set rom=concat(rom,'VII');
  47.             when '8' then set rom=concat(rom,'VIII');
  48.             when '9' then set rom=concat(rom,'IX');
  49.         end case;
  50.        
  51.        return rom;
  52.     end
  53. *
  54. -- Ejemplo
  55. select dec_a_romanos(75);
Advertisement
Add Comment
Please, Sign In to add comment