?
Solved

Query date column in Oracle

Posted on 2013-05-16
8
Medium Priority
?
527 Views
Last Modified: 2013-05-20
Hello,
I need to query an oracle table by date and am not having any luck.  I want to search by date, date only.  I have a table with a date column and in the date column is the following data:

08/27/2012 8:16:00 AM

I want to query by date and enter '8/27/2012' or '08/27/2012' and yield the record.  However, the record is not found unless I enter the time as well, or use a between that includes time.  Any help you can provide is much appreciated.  

select from table where date .....'8/27/2012'
0
Comment
Question by:newtoperlpgm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 39172852
select * from yourtable
where yourdate >= date '2012-08-27'
and yourdate < date '2012-08-27' + 1
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 1600 total points
ID: 39172856
if you don't want to use ansi/iso date literals then use to_date with explicit format


select * from yourtable
where yourdate >= to_date('8/27/2012','mm/dd/yyyy')
and yourdate < to_date('8/27/2012','mm/dd/yyyy') + 1
0
 
LVL 3

Expert Comment

by:oratech
ID: 39172926
If you know you won't be passing in the time then you could truncate the date in the select:

select * from yourtable
where trunc(yourdate) = to_date('8/27/2012','mm/dd/yyyy')

Alternatively... You could pass in your date in the default database date format.  

Check the format like this:

SHOW PARAMETER NLS_DATE_FORMAT

In my case it is DD-MON-RR so my query would look like this:

select * form yourtable
where trunc(yourdate) = '27-AUG-2012'
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:newtoperlpgm
ID: 39173108
Hello,
So let's see, if I need to put this into a parameterized query, it should work like the following:?

select * from mytable
where mydate >= date parameter
and mydate < date parameter + 1

Thanks.
0
 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 400 total points
ID: 39173372
yes, if using string parameters

note however that syntax requires the date string you pass to be 'yyyy-mm-dd'

the alternative is to use the to_date() function which permits you to explictly state the date part sequence

select * from mytable
where mydate >= to_date(  parameter ,'mm/dd/yyyy')
and mydate  < to_date( parameter ,'mm/dd/yyyy') + 1
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39173380
btw: I advise against applying TRUNC() to data for filtering if it can be avoided

It's not 'wrong' as such, it just adds overheads to a query that are avoidable

My view is "adjust the filter criteria to suit the data, not vice-versa", so when your data has dates (with or without time) use the >= with < approach. You get the wanted results and you haven't changed to data to get there.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 39174846
this syntax would only work if "parameter" was actually the literal values as shown above.

Not variables

select * from mytable
where mydate >= date parameter
and mydate < date parameter + 1

Open in new window



if you have variables then use to_date as shown above in the 2nd post and later again in portletpaul's post
0
 

Author Comment

by:newtoperlpgm
ID: 39182761
I tried

 select * from mytable
where mydate >= to_date(  parameter ,'mm/dd/yyyy')
and mydate  < to_date( parameter ,'mm/dd/yyyy') + 1

and it seems to work, at least for the test data I am using.  Thanks for all the input and help.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question