Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare Oracle Date with C# DateTime

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)

like image 410
esastincy Avatar asked Aug 26 '11 20:08

esastincy


1 Answers

You can use the Oracle TO_DATE function.

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

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 - 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:

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.

like image 122
Tridus Avatar answered Oct 12 '22 14:10

Tridus