Solved

Query date column in Oracle

Posted on 2013-05-16
8
526 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 400 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

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 100 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video shows how to recover a database from a user managed backup

726 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