0

I have this DATETIME field in my oracle database shown in the image

enter image description here

I'm trying to make a query which returns something between specific dates but this query returns nothing.

 SELECT * 
 FROM tbl_meter 
 WHERE TO_DATE(DATETIME,'DD/MM/YYYY') BETWEEN '%s' AND '%s'

What am I missing?

EDIT:

SELECT * FROM tbl_meter WHERE DATETIME BETWEEN '15/01/2014' AND '07/01/2014'

enter image description here

5
  • What values are you passing in %s ? What is data type of DATETIME column? Commented Jan 14, 2014 at 8:47
  • Which datatype has that datetime column? If it is a DATE column, the likeoperator does not make any sense (plus applying to_date() on a date column to convert it to a date doesn't make sense) Commented Jan 14, 2014 at 8:51
  • %s -> dd/mm/yyyy format. And DATETIME is a timestamp in database Commented Jan 14, 2014 at 8:52
  • Applying to_date() on a timestamp column is totally useless. to_date() converts a string into a date. When you apply it on a timestamp column, the timestamp first gets converted into a string (subject to implicit data type conversion based on various NLS settings) which then gets converted back to a date value. Simply use where datetime between %1 and %2 and you should be fine. You maybe want to apply the trunc() function on the value to remove the time part. Commented Jan 14, 2014 at 8:55
  • Trying where datetime between %1 and %s2 returns nothing. If you can see my edit Commented Jan 14, 2014 at 9:00

3 Answers 3

2
SELECT * 
FROM tbl_meter 
WHERE TRUNC(DATETIME) BETWEEN to_date('%s','DD/MM/YYYY') AND to_date('%s','DD/MM/YYYY');

Asssuming DATETIME is of DATE data type.

If it is of TIMESTAMP datatype then,

SELECT * 
FROM tbl_meter 
WHERE TRUNC(CAST(DATETIME AS DATE)) BETWEEN to_date('%s','DD/MM/YYYY') AND to_date('%s','DD/MM/YYYY');

If it is VARCHAR, then

SELECT * 
FROM tbl_meter 
WHERE TRUNC(To_DATE(DATETIME,'Dd/MM/YYYY HH24:MI:SS.FF6')) BETWEEN to_date('%s','DD/MM/YYYY') AND to_date('%s','DD/MM/YYYY');
Sign up to request clarification or add additional context in comments.

6 Comments

Your TIMESTAMP query has a syntax error. ora-00907 missing parantesis
Does first %s always have to be smaller then the second %s? Otherwise query returns empty
Uhm, interesting fact. In mySQL it doesn't matter which one is smaller.
Yup, Oracle optimzer converts, between into >= and <= respectively for the values. It doesnt validate which of the parameter is bigger! May be MySQL is designed to handle that..
You don't need to explicitly cast in the second version, but it doesn't hurt and I guess spells out what's happening. (The value returned is always of data type DATE, even if you specify a different datetime data type for date).
|
2

It looks like you are using the same parameter twice. I expect you to have

select * 
from   tbl_meter 
where  to_date(datetime, 'DD/MM/YYYY') between to_date('%s1', 'DD/MM/YYYY') and to_date('%s2', 'DD/MM/YYYY')

9 Comments

%s specifies a string variable is expected. I pass parameters to %s
What are the value(s) for %s then? If %s is a string, shouldn't you do to_date('%s') then?
value(s) for %s is something like 17/10/2013 and 19/11/2013 but those values comes from JSON as strings
If you write this query out (without using parameters), does it work then? If so, this is a program error. If not, you have to take a look at the SQL expression.
It returns empty in pl/sql without using parameters if you can see my edit
|
-1

Try this way

SELECT * 
FROM tbl_meter 
WHERE TO_DATE(DATETIME,'DD/MM/YYYY') >='%s' AND TO_DATE(DATETIME,'DD/MM/YYYY') <'%s'

2 Comments

There is no difference in the use of between or your query.
I know but it is just a thought we can give a try if it works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.