我有一个数据结果,我希望按两个不同的日期过滤。
以下是有效的,除非completion_date为null。如果它为null我想使用一个名为created_date的不同字段。
string strSQL = string.Format(@"select * from my_table
where user_Id = {0}
AND [completion_date] between {1} AND {2}
order by completion_status desc, [completion_date] asc"
, userId, dateStart, dateEnd, visibilityIndicator);
也许是这样的:
string strSQL = string.Format(@"select * from my_table
where user_Id = {0}
CASE completion_date
WHEN is null THEN [completion_date] between {1} AND {2}
ELSE [created_date] between {1} AND {2}
END
AND
order by completion_status desc, [completion_date] asc"
, userId, dateStart, dateEnd, visibilityIndicator);
检查该空值然后切换到created_date的最佳方法是什么?
答案 0 :(得分:3)
where isnull(completetion_date,created_date) between {1} and {2}
应该有效
答案 1 :(得分:0)
string strSQL = string.Format(@"select * from my_table
where user_Id = {0}
AND ISNULL([completion_date],[created_date]) between {1} AND {2}
order by completion_status desc, [completion_date] asc"
答案 2 :(得分:0)
ANSI SQL兼容版本:
SELECT * FROM my_table WHERE user_Id = {0}
AND [completion_date] BETWEEN {1} AND {2}
OR ([completion_date] IS NULL AND [created_date] BETWEEN {1} AND {2})
ORDER BY completion_status DESC, [completion_date] ASC