| Oracle Dates query [message #648265] |
Fri, 19 February 2016 08:46  |
anil_mk
Messages: 146 Registered: August 2006 Location: Bangalore, India
|
Senior Member |

|
|
Hi All,
Need your help, please fnd below code and my requirement
create table TMP_TEST (id number,DATES date)
/
insert into TMP_TEST values(1,'10-FEB-2016')
/
insert into TMP_TEST values(2,'12-FEB-2016')
/
insert into TMP_TEST values(3,'14-FEB-2016')
/
insert into TMP_TEST values(4,'17-FEB-2016')
/
commit
/
/*SELECT * FROM TMP_TEST
ID DATES
-------------------------------
1 10-02-16
2 12-02-16
3 14-02-16
4 17-02-16
*/
select * from TMP_TEST where DATES='10-FEB-2016'; --It returns row which has dates='12-Feb-2016' data
/*
ID DATES
-------------------------------
1 10-02-16
*/
when I pass values for column Dates as '09-Feb-2016' in where clause, query should return data for 10-FEB-2016
/*
ID DATES
-------------------------------
1 10-02-16
*/
when I pass values for column Dates as '11-Feb-2016', query should return data for 10-Feb-2016
when I pass values for column Dates as '18-Feb-2016', query should return data for 17-FEB-2016
How to handle this in SQL?
Thanks,
Anil MK
|
|
|
|
|
|
|
|
|
|
|
|
| Re: Oracle Dates query [message #648273 is a reply to message #648265] |
Fri, 19 February 2016 10:01   |
 |
Michel Cadot
Messages: 68776 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
Wrong test case:
SQL> create table TMP_TEST (id number,DATES date)
2 /
Table created.
SQL> insert into TMP_TEST values(1,'10-FEB-2016')
2 /
insert into TMP_TEST values(1,'10-FEB-2016')
*
ERROR at line 1:
ORA-01858: a non-numeric character was found where a numeric was expected
Quote:when I pass values for column Dates as '09-Feb-2016' in where clause, query should return data for 10-FEB-2016
when I pass values for column Dates as '11-Feb-2016', query should return data for 10-Feb-2016
when I pass values for column Dates as '18-Feb-2016', query should return data for 17-FEB-2016
How to handle this in SQL?
1/ This will surely not work in my country, anyway
2/ Explain the rule, what is the result if you pass 10/11/2012? and 28/02/2016? and 13/02/2016?
[Updated on: Fri, 19 February 2016 10:02] Report message to a moderator
|
|
|
|
|
|
|
|
| Re: Oracle Dates query [message #648283 is a reply to message #648278] |
Fri, 19 February 2016 11:16   |
Solomon Yakobson
Messages: 3310 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
anil_mk wrote on Fri, 19 February 2016 12:05
General rule is if we pass value which is exists in table then query should retrieve corresponding row.
If passed value doesn't match then query should consider nearest value and return that row.
with t as (
select id,
dates,
dense_rank() over(order by abs(dates - date '&dt')) drnk
from tmp_test
)
select id,
dates
from t
where drnk = 1
/
Enter value for dt: 2016-02-14
old 4: dense_rank() over(order by abs(dates - date '&dt')) drnk
new 4: dense_rank() over(order by abs(dates - date '2016-02-14')) drnk
ID DATES
---------- ---------
3 14-FEB-16
SQL> /
Enter value for dt: 2016-02-09
old 4: dense_rank() over(order by abs(dates - date '&dt')) drnk
new 4: dense_rank() over(order by abs(dates - date '2016-02-09')) drnk
ID DATES
---------- ---------
1 10-FEB-16
SQL>
SY.
|
|
|
|
| Re: Oracle Dates query [message #648284 is a reply to message #648283] |
Fri, 19 February 2016 11:18   |
Solomon Yakobson
Messages: 3310 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
Keep in mind, query returns all nearest values:
SQL> with t as (
2 select id,
3 dates,
4 dense_rank() over(order by abs(dates - date '&dt')) drnk
5 from tmp_test
6 )
7 select id,
8 dates
9 from t
10 where drnk = 1
11 /
Enter value for dt: 2016-02-13
old 4: dense_rank() over(order by abs(dates - date '&dt')) drnk
new 4: dense_rank() over(order by abs(dates - date '2016-02-13')) drnk
ID DATES
---------- ---------
2 12-FEB-16
3 14-FEB-16
SQL>
SY.
|
|
|
|
| Re: Oracle Dates query [message #648285 is a reply to message #648284] |
Fri, 19 February 2016 11:22   |
Solomon Yakobson
Messages: 3310 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
If you want nearest higher dates:
SQL> with t as (
2 select id,
3 dates,
4 dense_rank() over(order by dates) drnk
5 from tmp_test
6 where dates >= date '&dt'
7 )
8 select id,
9 dates
10 from t
11 where drnk = 1
12 /
Enter value for dt: 2016-02-13
old 6: where dates >= date '&dt'
new 6: where dates >= date '2016-02-13'
ID DATES
---------- ---------
3 14-FEB-16
SQL>
SY.
|
|
|
|
|
|
| Re: Oracle Dates query [message #648287 is a reply to message #648278] |
Fri, 19 February 2016 11:54  |
 |
Michel Cadot
Messages: 68776 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
Quote:General rule is if we pass value which is exists in table then query should retrieve corresponding row.
What does mean "corresponding"?
Why didn't you the row for the "dates" I gave?
Quote:Below commented lines are copied from my SQLPLUS after running.
It does not work for me, so it is wrong.
You MUST post a test case which works for EVERYONE not just for you.
You MUST learn the difference between a date and a string.
This is the FIRST things you have to know, the rest, and this topic, is useless for you until you understand that.
This is basic, stay away from Oracle and any database until you understand the basics.
|
|
|
|