Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

mysql - Como concatenar strings e converter para datar as strings?


CONCAT() é a chave.

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)

DEPOIS:

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)