Solved

Query date column in Oracle

Posted on 2013-05-16
8
525 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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 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 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

Suggested Solutions

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

730 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