我试图在定义句点的两个句点参数为空时给出所有记录的查询,并且只选择参数不为空时我需要的记录。
这两个参数是@PeriodFrom
和@PeriodeUntil
我试图做像
SELECT DISTINCT Field
FROM Table
WHERE StartDate BETWEEN
CASE WHEN @PeriodeFrom IS NULL AND @PeriodeTot IS NULL
THEN StartDatum AND CAST(GETDATE() AS Date)
ELSE @PeriodeVan
AND @PeriodeTot
这个没有用。
答案 0 :(得分:0)
你可以用简单的布尔逻辑
来做到这一点SELECT DISTINCT Field
FROM Table
WHERE (@PeriodFrom IS NULL AND @PeriodUntil IS NULL)
OR (StartDate BETWEEN @PeriodFrom AND @PeriodUntil)
答案 1 :(得分:0)
首先处理空参数,然后处理范围。
…
where (@PeriodeFrom is null and @PeriodeUntil is null)
or StartDatum between @PeriodeFrom and @PeriodeUntil
答案 2 :(得分:0)
使用ISNULL:
SELECT DISTINCT Field
FROM [Table]
WHERE StartDate BETWEEN isnull(@PeriodeFrom, StartDate) AND isnull(@PeriodeTot, Startdate)