SQL 检查空日期然后大小写切换

发布于 2025-01-08 08:55:22 字数 912 浏览 0 评论 0原文

我有一个数据结果,我想按两个不同的日期进行过滤。

除非completion_date 为空,否则以下内容正常工作。如果它为空,我想使用另一个名为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的最佳方法是什么?

I have a data result that I want to filter by two different dates.

The below is working except when completion_date is null. If it is null I want to use a different field called 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);

Perhaps something like this:

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);

What is the best way to check for that null value and then switch to created_date?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

韶华倾负 2025-01-15 08:55:22
where isnull(completetion_date,created_date) between {1} and {2}

应该有效

where isnull(completetion_date,created_date) between {1} and {2}

should work

℡寂寞咖啡 2025-01-15 08:55:22
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"
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"
巨坚强 2025-01-15 08:55:22

符合 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

An ANSI SQL compliant version:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文