Solved

Query date column in Oracle

Posted on 2013-05-16
8
521 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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 73

Expert Comment

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

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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 48

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 48

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 73

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now