0
0

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 1 year has passed since last update.

日付、曜日のsql - その月の第1月曜

Posted at

日付、曜日で使うものメモ (snowflake)

-- 日本時間の今日
SET TODAY = TO_DATE(convert_timezone('Asia/Tokyo', current_timestamp()));

SELECT
-- 曜日 str
to_varchar($TODAY, 'DY'),

-- 出力曜日は WEEK_START セッションパラメーターによって制御  これは0又は1の場合
-- その週の月曜日
DATE_TRUNC('week', $TODAY),
-- その週の日曜日
LAST_DAY($TODAY, 'week'),

-- 第一月曜
DATEADD(day,-TRUNC((DATE_PART(day,DATE_TRUNC('week', $TODAY))-1)/7)*7, DATE_TRUNC('week', $TODAY)),

-- その月の最後の日
LAST_DAY($TODAY),
LAST_DAY($TODAY, 'month'),
-- その年の最後の日
LAST_DAY($TODAY, 'year')
;

snowflakeの"WEEK_START"について

https://docs.snowflake.com/ja/sql-reference/parameters#label-week-start
https://docs.snowflake.com/ja/sql-reference/functions-date-time#label-calendar-weeks-weekdays

SELECT DAYOFWEEK(convert_timezone('Asia/Tokyo', current_timestamp()));

これで今日の曜日が何でかえってくるかでわかる。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?