Solved

How do you use the where clause with date

Posted on 2014-09-17
2
237 Views
Last Modified: 2014-09-17
I'm new at SQL so I need a little help with being able to pick dates.
I have a table that I'm running a select query against
SELECT arcusto_id, invoice_date
FROM arinvoice
WHERE arcusto_id = 25434
The result are something like this
25434         9/4/2014
25434         9/11/2014
25434         9/17/2014
Now I want to run the query by certain date
SELECT arcusto_id, invoice_date
FROM arinvoice
WHERE arcusto_id = 25434 AND invoice_date = '9/11/2014'
This is the result I would expect
25434        9/11/2014
But I get nothing but an  error telling me the month is not valid. I also tried 09 instead of 9.
What am i doing wrong
0
Comment
Question by:dkma2010
2 Comments
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 500 total points
Comment Utility
Hello,

to query dates from oracle you need to use  TO_DATE function so you query should be like

SELECT arcusto_id, invoice_date
FROM arinvoice
WHERE arcusto_id = 25434 AND invoice_date = to_date('9/11/2014','mm/dd/yyyy')

Open in new window

0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
To clarify the above post:
ALWAYS get in the habit of explicit data conversions.  Use TO_CHAR when querying a date column to ensure the result is ALWAYS exactly what you want.  Use TO_DATE when querying to make sure you get what you want.

Also make sure your invoice_date column doesn't have time portions.  Dates in Oracle have a time built in.  If the app developers didn't account for them, the column values may have them by 'accident'.

If they have times, the above query won't work.  You'll need to use '>=' and '<'.

To confirm this:
SELECT arcusto_id, to_char(invoice_date,'MM/DD/YYYY HH24:MI:SS')
FROM arinvoice
WHERE arcusto_id = 25434


If all the time portions are zeros, the above query should work fine.
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.

Join & Write a Comment

Suggested Solutions

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be ā€¦
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the dā€¦
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.
Via a live example, show how to take different types of Oracle backups using RMAN.

743 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

13 Experts available now in Live!

Get 1:1 Help Now