比较Oracle Date与C#DateTime [英] Compare Oracle Date with C# DateTime

查看:85
本文介绍了比较Oracle Date与C#DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个内联SQL查询(无需对此进行评论,我知道它不是最好的方法,但是它是公司的工作方式!),我需要将Oracle DATE列与C#DateTime.Now进行比较.为了使此比较生效,我需要在DateTime.Now和该列周围进行哪些转换? (我想比较整个DateTime对象,而不仅仅是Date部分)

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)

推荐答案

您可以使用Oracle TO_DATE 功能.

You can use the Oracle TO_DATE function.

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

有关c#日期(请参见自定义日期和时间字符串):

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

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

如果要增加小时/分钟/秒,则可以进行相应的修改.

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

edit-实际上,因为您提到了整个事情,而不仅仅是日期,所以我将其添加. :)

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.

要使用where子句构建字符串,请执行以下操作:

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

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

如果要使用特定日期而不是DateTime.现在,只需将保存该日期的变量放在那里.

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

这篇关于比较Oracle Date与C#DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆