我们的从属表中的一个仅在每月的第三天提供,我需要在前两天编写一个查询,该查询应检查从第3个病房获得的当前月份数据。
尝试了CASE
表达式,该表达式不起作用
select case when datepart(day, getdate()) <= 2
then (select * from T1 where date in (201904,201903))
else (select * from T1 where date in (201905,201904))
end
我知道我的方法是错误的,需要帮助才能达到我的要求。
答案 0 :(得分:0)
有点猜测,但是像这样?
SELECT {Columns}
FROM {Your Table} YT
WHERE YourDateColumn >= DATEADD(MONTH, DATEDIFF(MONTH, 0, DATEADD(DAY, -2, GETDATE())),0)
AND YourDateColumn < DATEADD(MONTH, DATEDIFF(MONTH, 0, DATEADD(DAY, -2, GETDATE()))+1,0);
答案 1 :(得分:0)
无论是否在前三天内,您都可以使用它来“标记”。 它的格式设置为将日期看成INT。
DECLARE @Currentday AS INT
SET @Currentday = cast(replace(cast(GETDATE() AS DATE), '-', '') AS INT)
IF @Currentday BETWEEN cast(replace(cast(DATEADD(month, DATEDIFF(month, 0, getdate()), 0) AS DATE), '-', '') AS INT)
AND cast(replace(cast(DATEADD(month, DATEDIFF(month, 0, getdate()), 2) AS DATE), '-', '') AS INT)
-- if within first 3 days
SELECT 1 --insert your own statement
ELSE
-- if not within first 3 days
SELECT 2 --insert your own statement