必要なものは次のとおりです。
select
concat(yr,'-',if(leapyear=0,if(right(dt,5)='02-29','03-01',
right(@mydate,5)),right(@mydate,5))) dttm
from
(
select concat(if(my_md > md,yr -1,yr),'-',my_md) dt,yr,my_md,
IF(MOD(yr,4)>0,0,IF(MOD(yr,100),1,(MOD(yr,400)=0))) leapyear FROM
(select substr(date(@mydate),6) my_md) AA,
(select substr(date(now()),6) md,year(now()) yr) BB
) A;
例として選択した 3 つの日付と 2 つのうるう年の日付を次に示します。
set @mydate = '2012-01-01';
set @mydate = '2010-05-05';
set @mydate = '2012-10-10';
set @mydate = '2008-02-29';
set @mydate = '2012-02-29';
ここで実行されます
mysql> set @mydate = '2012-01-01'; クエリ OK、影響を受ける行は 0 (0.00 秒)
mysql> select
-> concat(yr,'-',if(leapyear=0,if(right(dt,5)='02-29','03-01',right(@mydate,5)),right(@mydate,5))) dttm
-> from
-> (
-> select concat(if(my_md > md,yr -1,yr),'-',my_md) dt,yr,my_md,
-> IF(MOD(yr,4)>0,0,IF(MOD(yr,100),1,(MOD(yr,400)=0))) leapyear FROM
-> (select substr(date(@mydate),6) my_md) AA,
-> (select substr(date(now()),6) md,year(now()) yr) BB
-> ) A;
+------------+
| dttm |
+------------+
| 2012-01-01 |
+------------+
1 row in set (0.00 sec)
mysql> set @mydate = '2010-05-05';
Query OK, 0 rows affected (0.00 sec)
mysql> select
-> concat(yr,'-',if(leapyear=0,if(right(dt,5)='02-29','03-01',right(@mydate,5)),right(@mydate,5))) dttm
-> from
-> (
-> select concat(if(my_md > md,yr -1,yr),'-',my_md) dt,yr,my_md,
-> IF(MOD(yr,4)>0,0,IF(MOD(yr,100),1,(MOD(yr,400)=0))) leapyear FROM
-> (select substr(date(@mydate),6) my_md) AA,
-> (select substr(date(now()),6) md,year(now()) yr) BB
-> ) A;
+------------+
| dttm |
+------------+
| 2012-05-05 |
+------------+
1 row in set (0.00 sec)
mysql> set @mydate = '2012-10-10';
Query OK, 0 rows affected (0.00 sec)
mysql> select
-> concat(yr,'-',if(leapyear=0,if(right(dt,5)='02-29','03-01',right(@mydate,5)),right(@mydate,5))) dttm
-> from
-> (
-> select concat(if(my_md > md,yr -1,yr),'-',my_md) dt,yr,my_md,
-> IF(MOD(yr,4)>0,0,IF(MOD(yr,100),1,(MOD(yr,400)=0))) leapyear FROM
-> (select substr(date(@mydate),6) my_md) AA,
-> (select substr(date(now()),6) md,year(now()) yr) BB
-> ) A;
+------------+
| dttm |
+------------+
| 2012-10-10 |
+------------+
1 row in set (0.00 sec)
mysql> set @mydate = '2008-02-29';
Query OK, 0 rows affected (0.00 sec)
mysql> select
-> concat(yr,'-',if(leapyear=0,if(right(dt,5)='02-29','03-01',right(@mydate,5)),right(@mydate,5))) dttm
-> from
-> (
-> select concat(if(my_md > md,yr -1,yr),'-',my_md) dt,yr,my_md,
-> IF(MOD(yr,4)>0,0,IF(MOD(yr,100),1,(MOD(yr,400)=0))) leapyear FROM
-> (select substr(date(@mydate),6) my_md) AA,
-> (select substr(date(now()),6) md,year(now()) yr) BB
-> ) A;
+------------+
| dttm |
+------------+
| 2012-02-29 |
+------------+
1 row in set (0.00 sec)
mysql> set @mydate = '2012-02-29';
Query OK, 0 rows affected (0.00 sec)
mysql> select
-> concat(yr,'-',if(leapyear=0,if(right(dt,5)='02-29','03-01',right(@mydate,5)),right(@mydate,5))) dttm
-> from
-> (
-> select concat(if(my_md > md,yr -1,yr),'-',my_md) dt,yr,my_md,
-> IF(MOD(yr,4)>0,0,IF(MOD(yr,100),1,(MOD(yr,400)=0))) leapyear FROM
-> (select substr(date(@mydate),6) my_md) AA,
-> (select substr(date(now()),6) md,year(now()) yr) BB
-> ) A;
+------------+
| dttm |
+------------+
| 2012-02-29 |
+------------+
1 row in set (0.00 sec)
mysql>
試してみる !!!