3
1

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 3 years have passed since last update.

Athenaで曜日を番号で取得する

Last updated at Posted at 2021-04-25

Athenaで日付から曜日を取得したい

Athenaのprestoベースなのでdate_formatのprestoのドキュメントを参照すると

%w Day of the week (0 .. 6), where Sunday is the first day of the week 3

とあるためたとえば2021/04/25の日曜日の番号を取得するには

select date_format(timestamp '2021-04-25', '%w')

で取得可能と思われる。
しかし、実際に実行してみると

INVALID_FUNCTION_ARGUMENT: %w not supported in date format string

とエラーが発生される。Athenaでは%wがサポートされていないようです。

day_of_weekの利用

day_of_weekを利用すると曜日番号が取得可能です。

select day_of_week(timestamp '2021-04-25') 

これで番号が取得可能なのですが、day_of_weekは

Returns the ISO day of the week from x. The value ranges from 1 (Monday) to 7 (Sunday).

とあるため月曜から土曜日は1~6でdate_formatと同じなのですが、日曜日だけ0ではなく7が返ってきてしまいます。
従って0~6で取得したい場合は

select if (day_of_week(timestamp '2021-04-25')  = 7, 0, day_of_week(timestamp '2021-04-25'))

とif文で7の場合に0に置き換えます。

他にいいやり方あったら教えて下さい!

3
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?