Question

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?

Was it helpful?

Solution

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()))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top