質問

I'm trying to create a SQL statement (in H2 Dialect) that creates a DATE which is the tuesday in the "week-of-year" that is two weeks from the current week.

For example, given CURRENT_DATE() returns 2014-04-23, the date that I want would be 2014-04-08, which is the Tuesday in the week that is two weeks before the week the Date "2014-04-23" is in.

How can I express this with H2's Date and Time functions?

役に立ちましたか?

解決

In I would do it like this

DECLARE @date DATE = GETDATE();
SELECT DATEADD(DAY,3-DATEPART(WEEKDAY,@date),DATEADD(WEEK,-2,@date))

Where '3' represents day of the week (Tuesday) and '-2' represents subtracting two weeks.

So now in it is very similar

SELECT DATEADD('DAY',3-DAY_OF_WEEK(CURRENT_DATE()),DATEADD('WEEK',-2,CURRENT_DATE()))
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top