Solved

How do you use the where clause with date

Posted on 2014-09-17
2
242 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
[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
2 Comments
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 500 total points
ID: 40329142
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 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40329172
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

732 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