1

i have the following query which does not retrieve data even though there is:

select *
from INTERFACE_RUN
where TO_CHAR(INTERFACE_RUN.START_TIME, 'dd-mon-yy') = '04-MAY-10';

The start_time field is a timestamp field. Hence I converted it using to_char and compared with the user passed value of 04-MAY-10. But it did not fetch any data. Any help please. Thanks in advance

5
  • 1
    use to_date to convert timestamp to date. Commented Jun 13, 2016 at 14:15
  • Did you mean the following: select * from INTERFACE_RUN where TO_DATE(INTERFACE_RUN.START_TIME, 'dd-mon-yy') = '04-MAY-10'; Commented Jun 13, 2016 at 14:21
  • And does SELECT TO_CHAR(INTERFACE_RUN.START_TIME, 'dd-mon-yy') FROM INTERFACE_RUN show the values you expect? Commented Jun 13, 2016 at 14:21
  • The query runs but does not fetch any data even after the data does exist Commented Jun 13, 2016 at 14:23
  • I beg your pardon? SELECT TO_CHAR(INTERFACE_RUN.START_TIME, 'dd-mon-yy') FROM INTERFACE_RUN does not return any row? Then your table is clearly empty, there's no way a WHERE clause can change that. Please double-check you're connecting to the correct server and database and typing the correct table name. Commented Jun 14, 2016 at 10:16

2 Answers 2

6

to_char() pays attention to the case of the pattern. So, you are producing `04-may-10', which is not the same.

So, try this:

where TO_CHAR(INTERFACE_RUN.START_TIME, 'DD-MON-YY') = '04-MAY-10';

That said, I much prefer:

where trunc(INTERFACE_RUN.START_TIME) = date '2010-05-04'

or:

where INTERFACE_RUN.START_TIME >= date '2010-05-04' AND
      INTERFACE_RUN.START_TIME < (date '2010-05-04') + 1

Why? For two reasons. First, the column is a date, so I prefer date comparisons. Second, I prefer the ANSI/ISO standard date formats. The second version can also readily take advantage of an index on the START_TIME column.

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

6 Comments

Hey thanks a lot. The format of 'DD-MON-YY' did work.
@Aniket why are you persisting in using 2-digit years? Years have got 4 digits; why introduce ambiguity when you don't have to?
Because the data entries are present in that fashion. I cannot compare YYYY with YY
@Aniket . . . Don't confuse the fact that Oracle prints out a date column as DD-MON-YY. In general, Oracle is keeping the full date.
@Aniket To add to what Gordon said, if the column is of DATE datatype, then Oracle is storing information about the whole date, from the full year down to seconds. When it is displayed, it has to be converted to a string. You can explicitly set the format you want it to be displayed in (e.g.: select to_char(sysdate, 'hh24:mi yyyy dd mm Day Month') from dual;) or you can rely on the nls_date_format parameter to automatically handle the conversion; this defaults to a format of DD-MON-RR. You can change that by doing alter session set nls_date_format = '<format>';`.
|
4

Oracle will convert the date to lowercase, thus generating '04-may-10'
Try with 'DD-MON-YY' (uppercase)

select TO_CHAR(sysdate, 'dd-mon-yy') from dual
union all
select TO_CHAR(sysdate, 'dd-MON-yy') from dual;

TO_CHAR(S
---------
13-jun-16
13-JUN-16

1 Comment

Hey thanks a lot. The format of 'DD-MON-YY' did work.

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.