Comparing Oracle dates with C # DateTime

I am creating an inline SQL query (no need to comment on this. I know that this is not the best method, but its a way to create a company!), And I need to compare the Oracle DATE column with C # DateTime.Now. What conversions do I need to set around DateTime.Now and the column to get this comparison? (I want to compare the entire DateTime object not only with the Date part)

+4
source share
1 answer

You can use the Oracle TO_DATE function.

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

For C # date (see custom date and time strings ):

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

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

edit - Actually, since you mentioned everything, not just the date, I will add. :)

 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 start it, but it should work.

To build a line with a where clause, you must do the following:

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

If you want to use a specific date, not DateTime.Now, just put a variable in its place that holds it there.

+11
source

Source: https://habr.com/ru/post/1369229/


All Articles