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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With