我想提取今天的日期,如果今天的日期是星期五,那么做(今天3)其他(今天-11)并在下面的删除声明中使用此查询的结果
这是我的查询:
Delete from table where date<(above result)
select now()::timestamp::date; this statement will give me the today's date
select to_char(now()::timestamp::date, 'dy') this will give me week day in string
select extract(dow from now()::timestamp) this will give me week day in integer
我如何将这些事情结合起来以达到我的要求。
答案 0 :(得分:0)
您可以尝试这样:
select case extract(dow from current_date) when 5
then (CURRENT_DATE - INTERVAL '3 day')
else
CURRENT_DATE - INTERVAL '11 day' end
<强> SQLFIDDLE DEMO 强>
答案 1 :(得分:0)
Rahul的答案是正确的,但如果你不想顺其自然的话
然后你可以使用CASE WHEN
SELECT CASE WHEN extract(dow from now()::timestamp) = 5
THEN (select now()::timestamp::date) - 3
ELSE (select now()::timestamp::date) - 11
END
希望它会有所帮助:)
答案 2 :(得分:0)
SELECT
CASE extract(dow FROM CURRENT_DATE)
WHEN 5
THEN (CURRENT_DATE - '3 day'::INTERVAL)
ELSE CURRENT_DATE - '11 day'::INTERVAL
END