Compare Oracle Date with C# DateTime

后端 未结 1 1479
南旧
南旧 2021-01-03 04:12

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 c

相关标签:
1条回答
  • 2021-01-03 04:50

    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.

    0 讨论(0)
提交回复
热议问题