Question

I am executing the below query,It returns me the blank row.However there are records in the table having upd_time = '12-MAR-08'.I don't understand why it is not returning the date '12-MAR-08'.Please help me out??

SELECT br_data.upd_time FROM BANKREC.br_data where br_data.upd_time = '12-MAR-08';

Was it helpful?

Solution

i don't have access to an oracle db at the moment but i remember using to_char.

try

SELECT br_data.upd_time FROM BANKREC.br_data where to_char(br_data.upd_time, 'DD-MON-YY') = '12-MAR-08';

OTHER TIPS

It's likely that upd_time isn't exactly 12-MAR-08. The date format is not showing the time component, but it's probably there (DATE data type in Oracle can contain both date and time components).

Try this (it will allow you to see the time components):

alter session set nls_date_format='DD-MON-YY HH24:MI:SS';

SELECT br_data.upd_time FROM BANKREC.br_data
where br_data.upd_time >= to_date('12-MAR-08','DD-MON-YY')
and br_data.upd_time < to_date('13-MAR-08','DD-MON-YY');

Is it the same if you do a

SELECT br_data.upd_time FROM BANKREC.br_data 
       where trunc(br_data.upd_time) = '12-MAR-08';

It could be that the upd_time is not a date, but a timestamp, so it actually contains for instance '12-MAR-08 05:30' which wouldn't be the same thing. trunc() removes the time part of the timestamp.

Since it doesn't return an error, I assume that it parses the date correctly, but otherwise you could try with to_date('12-MAR-08','DD-MON-YY')

You should use Oracle's function to convert your string properly into a date using

to_date('12-MAR-08', 'DD-MMM-YY')

Then you have to take into account that the Oracle "Date" datatype also contains time information to the nearest second. This means that the date that was constructed in the first step is actually midnight on March 12th. So you have to make sure that the upd_time is truncated to midnight:

trunc(upd_time, 'DAY') = to_date('12-MAR-08', 'DD-MMM-YY')

Your full query becomes

SELECT br_data.upd_time 
FROM BANKREC.br_data 
WHERE trunc(upd_time, 'DAY') = to_date('12-MAR-08', 'DD-MMM-YY');

There are other ways to skin this cat (you could transfer your updTime column to a proper char field with to_char(upd_time, 'DD-MMM-YY')), but it's usually advisable make the data you are looking for similar to what you can find in the database as that increases your chances of using an index for the lookup.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top