我需要一些小帮助,我必须配置一个报告,其中客户只需要15天的数据。按照以下方式
如果月中的日期小于或等于15,那么如果日期超过15,那么日期范围将是1到15,那么日期范围将是16到30或31
答案 0 :(得分:2)
{{1}}
答案 1 :(得分:2)
您也可以尝试
SELECT * FROM <table> WHERE [date] between
case when DATEPART(DAY, GETDATE()) <=15 then DATEADD(month, DATEDIFF(month, 0, getdate()), 0) else DATEADD(month, DATEDIFF(month, 0, getdate()), 15) end
and
case when DATEPART(DAY, GETDATE()) <=15 then DATEADD(month, DATEDIFF(month, 0, getdate()), 15) else DATEADD(MONTH,1+DATEDIFF(MONTH,0,GETDATE()),-1) end
答案 2 :(得分:1)
此查询可能会为您提供正确的结果
SELECT * FROM TableName
WHERE 1 = (
CASE WHEN DAY(GETDATE()) <= 15 THEN
CASE WHEN (DateField BETWEEN CONVERT(DATETIME,CONVERT(VARCHAR,MONTH(DateField)) + '-' + '1-' + CONVERT(VARCHAR,YEAR(DateField)))
AND CONVERT(DATETIME,CONVERT(VARCHAR,MONTH(DateField)) + '-' + '15-' + CONVERT(VARCHAR,YEAR(DateField))))
THEN 1 ELSE 0 END
ELSE
CASE WHEN(DateField BETWEEN CONVERT(DATETIME,CONVERT(VARCHAR,MONTH(DateField)) + '-' + '16-' + CONVERT(VARCHAR,YEAR(DateField)))
AND CONVERT(DATETIME,CONVERT(VARCHAR,MONTH(DateField)) + '-' + CONVERT(VARCHAR,DAY(EOMONTH(DateField))) + '-' + CONVERT(VARCHAR,YEAR(DateField))))
THEN 1 ELSE 0 END
END)