Solved

Query date column in Oracle

Posted on 2013-05-16
8
523 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
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 a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.

809 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