将 Oracle 日期与 C# 日期时间进行比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7210147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:16:25  来源:igfitidea点击:

Compare Oracle Date with C# DateTime

c#oracledatetime

提问by esastincy

I am building an in-line SQL query (no need to comment about this I know its not the best method but its how the company does things!) and I need to compare an Oracle DATE column with C# DateTime.Now. What conversions do I need to put around the DateTime.Now and the column in order to get this comparison to work? (I want to compare the whole DateTime object not just the Date part)

我正在构建一个内嵌 SQL 查询(无需对此发表评论,我知道它不是最好的方法,而是公司如何做事!)并且我需要将 Oracle DATE 列与 C# DateTime.Now 进行比较。我需要在 DateTime.Now 和列周围进行哪些转换才能使这种比较起作用?(我想比较整个 DateTime 对象而不仅仅是 Date 部分)

回答by Tridus

You can use the Oracle TO_DATEfunction.

您可以使用 Oracle TO_DATE函数。

WHERE someDateColumn <= TO_DATE(c#_date, 'YYYY/MM/DD')

For the c# date (see custom date and time strings):

对于 c# 日期(请参阅自定义日期和时间字符串):

DateTime.Now.ToString("yyyy/MM/dd")

If you want to add hours/minutes/seconds, you can modify accordingly.

如果要添加小时/分钟/秒,可以相应修改。

edit - Actually since you mentioned the whole thing and not just the date, I'll add it. :)

编辑 - 实际上,既然你提到了整件事而不仅仅是日期,我会添加它。:)

WHERE someDateColumn <= TO_DATE(c#_date, 'YYYY/MM/DD HH24:MI:SS')
DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")

At home so I can't run it, but that should work.

在家里,所以我无法运行它,但这应该可行。

To build a string with the where clause, you'd do this:

要使用 where 子句构建字符串,您可以这样做:

string someQuery = "SELECT * FROM aTable WHERE someDateColumn <=  TO_DATE('" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "', 'YYYY/MM/DD')"

If you wanted to use a specific date rather then DateTime.Now, just put the variable holding it there instead.

如果您想使用特定日期而不是 DateTime.Now,只需将保存它的变量放在那里。