3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UnixTimeを日付型に変換する方法

Last updated at Posted at 2015-11-09

出来事

ミリ秒で保存されているカラムに対して日付型に変換して同じく日付型のデータと比較を行ないたい

やったこと

date.sql
select * 
from table
where 
	TO_DATE(CONVERT_TIMEZONE('JST', TIMESTAMP 'epoch'+(table.column/1000)*INTERVAL), 'MM-DD') = TO_DATE('2015-11-09', 'MM-DD');

※TIMEZONEが要らない場合は CONVERT_TIMEZONE はいらないかも。

ちなみに

MySQLで同じことしようとするとUnixtimeが0未満の場合、 Null が返ってくる

date.sql
mysql> select from_unixtime(0);
+---------------------+
| from_unixtime(0)    |
+---------------------+
| 1970-01-01 09:00:00 |
+---------------------+
1 row in set (0.00 sec)

mysql> select from_unixtime(-10000);
+-----------------------+
| from_unixtime(-10000) |
+-----------------------+
| NULL                  |
+-----------------------+
1 row in set (0.00 sec)

この組み合わせで知見を得たので次からは上手くやるでしょう

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?