我正在处理查询:
SELECT DISTINCT PointPerson
FROM DailyTaskHours
WHERE ActivityDate BETWEEN @startDateParam
AND @endDateParam
GROUP BY PointPerson
HAVING SUM([Hours]) > 0
ORDER BY PointPerson
这就是它的回归:
正如您所看到的,返回的第一个值是一个空字符串。如何防止在查询中返回此行。如果空不返回..
答案 0 :(得分:2)
您只需在WHERE
子句
SELECT DISTINCT PointPerson
FROM DailyTaskHours
WHERE ActivityDate BETWEEN @startDateParam AND @endDateParam
AND PointPerson <> ''
GROUP BY PointPerson
HAVING SUM([Hours]) > 0
ORDER BY PointPerson
这种情况有很多变化,具体取决于表的规范化程度。
如果您确定始终有空格,则可以使用:
AND PointPerson <> ''
如果可能有 NULL 值,最明智的是使用:
AND ISNULL(PointPerson, '') <> ''
如果可以在该字段中有空格(它们会作为空白填充,但仍然不会被第一个选项捕获),您可以使用附加条件以及前两个条件的组合,这提供了最安全的选择(涵盖大多数情况):
AND ISNULL(PointPerson, '') <> ''
AND LEN(LTRIM(PointPerson)) > 0
这种条件组合将帮助您避免使用NULL值,并通过使用LTRIM
修剪它们来消除多个空格,其结果必须至少包含一个字符。
答案 1 :(得分:1)
还要考虑空值(如果需要)
SELECT DISTINCT PointPerson
FROM DailyTaskHours
WHERE ActivityDate BETWEEN @startDateParam
AND @endDateParam
AND isnull(PointPersonm,'') <> ''
GROUP BY PointPerson
HAVING SUM([Hours]) > 0
ORDER BY PointPerson
答案 2 :(得分:1)
SELECT DISTINCT PointPerson
FROM DailyTaskHours
WHERE ActivityDate BETWEEN @startDateParam
AND @endDateParam
AND len(ltrim(rtrim(PointPerson)))>0
GROUP BY PointPerson
HAVING SUM([Hours]) > 0
ORDER BY PointPerson