Creating funcion for short date in spanish

Creating funcion for short date in spanish

Problem Description:

Hi guys this function gives me this error: "Something wrong in line 35." (RETURN diaCompleto;), I dont know whats wrong with the code below anyway can point me out in the correct direction, tyvm.

DELIMITER $$

CREATE FUNCTION DIA_FECHA(fecha date) 
RETURNS VARCHAR(8)
BEGIN

  DECLARE nomDia VARCHAR(6);
  DECLARE dia VARCHAR(2);
  DECLARE diaCompleto VARCHAR(8);

  SET dia = LPAD(DAY(fecha),2,'0');

  IF(DAYOFWEEK(fecha) = 1) THEN
    SET nomDia = 'DOM - ';

  IF(DAYOFWEEK(fecha) = 2) THEN
    SET nomDia = 'LUN - ';

  IF(DAYOFWEEK(fecha) = 3) THEN
    SET nomDia = 'MAR - ';

  IF(DAYOFWEEK(fecha) = 4) THEN
    SET nomDia = 'MIE - ';

  IF(DAYOFWEEK(fecha) = 5) THEN
    SET nomDia = 'JUE - ';

  IF(DAYOFWEEK(fecha) = 6) THEN
    SET nomDia = 'VIE - ';

  IF(DAYOFWEEK(fecha) = 7) THEN
    SET nomDia = 'SAB - ';

  SET diaCompleto = CONCAT(nomDia,dia);
  RETURN diaCompleto;

END$$
DELIMITER ;

Solution – 1

The reason for your syntax error is that you need to use END IF; after each IF/THEN. See examples in the manual: https://dev.mysql.com/doc/refman/8.0/en/if.html

I think you don’t need to write this function at all. Instead, you could use MySQL’s DATE_FORMAT() function. It supports locale-aware names for days of the week.

mysql> set lc_time_names = 'es_ES';

mysql> select date_format(curdate(), '%a - %d') as diaCompleto;
+-------------+
| diaCompleto |
+-------------+
| sáb - 19    |
+-------------+
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject