sql >> Base de Datos >  >> RDS >> Mysql

mysql - ¿Cómo concatenar cadenas y convertir para fechar las cadenas?

CONCAT() es la clave.

ANTES:

mysql> CREATE  PROCEDURE `getMonthlyTotalScore`(IN ninjaId int,  IN month int, IN year int)
        -> BEGIN
        ->     DECLARE startDate DATE;
        ->     DECLARE endDate DATE;
        ->     DECLARE maxDay INTEGER;
        -> 
        ->     SELECT year + '-' + month + '-01'; #NOTE THIS
        -> 
        ->     
        -> END;    
        -> |
    Query OK, 0 rows affected (0.00 sec)

    mysql> call getMonthlyTotalScore(1,5,2012);
        -> |
    +----------------------------+
    | year + '-' + month + '-01' |
    +----------------------------+
    |                       2016 |
    +----------------------------+
    1 row in set (0.00 sec)

DESPUÉS:

mysql> CREATE  PROCEDURE `getMonthlyTotalScore`(IN ninjaId int,  IN month int, IN year int)
    -> BEGIN
    ->     DECLARE startDate DATE;
    ->     DECLARE endDate DATE;
    ->     DECLARE maxDay INTEGER;
    -> 
    ->     SELECT CONCAT(year,'-',month,'-01'); # NOTE THIS
    -> 
    ->     
    -> END;    |
Query OK, 0 rows affected (0.00 sec)

mysql> call getMonthlyTotalScore(1,5,2012);
    -> |
+------------------------------+
| CONCAT(year,'-',month,'-01') |
+------------------------------+
| 2012-5-01                    |
+------------------------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)