3

I am trying to select data from my Oracle database BY DATE using C#. However I am always getting an empty data set although the same query string works just fine in Oracle SQL Developer

String Query = "Select position_date from position";
OracleDataAdapter adapter = new OracleDataAdapter(Query, ocon); 
adapter.Fill(ds, "table"); //where ds is a dataset 
PrintDataSet(ds);

returns

3/8/2011 12:00:00 AM.... and more

However, when I change my query to below, then there is no output!

String Query = "Select position_date from position 
where to_char(position_date, 'mm-dd-yyyy') = '05-17-2012'"

This query works fine in oracle sql developer. I've also tried trunc(sysdate) but nothing seems to work! :(

7
  • 1
    Bit rusty, but try this - "select * from position where position_date = to_date('05-17-2012', 'mm-dd-yyyy')" Commented May 17, 2012 at 22:19
  • Well 3/8/2011 is a completely different day than 05-17-2012. It does make sense to me that you are not getting anything back. And you should not convert the date to a string but the string to a date. Using position_date = to_date('05-17-2012', 'mm-dd-yyyy') will enable Oracle to use an index on position_date. Commented May 17, 2012 at 22:19
  • @horse, That may be so, but it is not an equivalent query. The posted query should get everything on 5/17/12, your query gets you everything on 5/17/12 at midnight. You probably meant TRUNC(position_date) = to_date('05-17-2012', 'mm-dd-yyyy') Commented May 17, 2012 at 22:27
  • What does 'query works fine in oracle sql developer' mean? Is it returning any data? Commented May 17, 2012 at 22:29
  • @DCookie: yes of course you are right. Commented May 17, 2012 at 22:37

2 Answers 2

1
select * from position where trunc(position_date) = to_date('05-17-2012', 'mm-dd-yyyy')

worked.

Thanks.

Sign up to request clarification or add additional context in comments.

Comments

0

If your dates have no time component (and if that is so then guarantee it with a check constraint), then:

Select position_date
from   position 
where  position_date = date '2012-05-17'

Otherwise:

Select position_date
from   position 
where  position_date >= date '2012-05-17' and 
       position_date <  date '2012-05-17' + 1

1 Comment

String Query = "Select position_date from position pos where pos.position_date >= date '2012-05-17' and pos.position_date < date '2012-05-17' + 1 and rownum < 11"; This does not return anything when used in C#, but returns 10 rows in Oracle sql developer...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.