Home » SQL & PL/SQL » SQL & PL/SQL » Oracle Dates query (ORACLE, 12.1.0.2.0, WINDOWS 10)
Oracle Dates query [message #648265] Fri, 19 February 2016 08:46 Go to next message
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 #648267 is a reply to message #648265] Fri, 19 February 2016 08:50 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member

The purpose of TO_CHAR is to display a DATE datatype in some desired format.
The purpose of TO_DATE is to convert string datatype to DATE datatype.
With Oracle characters between single quote marks are STRINGS!
'This is a string, 2009-12-31, not a date'
When a DATE datatype is desired, then use TO_DATE() function including format.

'10-11-12'
Which is correct DATE below for string above?
Oct. 11 2012
Nov. 10 2012
Nov. 12 2010
Dec. 11 2010
Oct. 12 2011
Dec. 10 2011
I'll give you 6 guesses, since the first 5 will be incorrect.
Re: Oracle Dates query [message #648269 is a reply to message #648267] Fri, 19 February 2016 09:00 Go to previous messageGo to next message
anil_mk
Messages: 146
Registered: August 2006
Location: Bangalore, India
Senior Member

Thanks for reply, we can change nls_date_format

alter session set nls_date_format = 'DD-MON-YYYY';


So that we can see data as below

/*SELECT * FROM TMP_TEST
ID DATES
-------------------------------
1 10-FEB-2016
2 12-FEB-2016
3 14-FEB-2016
4 17-FEB-2016
*/

Thanks,
Anil MK
Re: Oracle Dates query [message #648270 is a reply to message #648269] Fri, 19 February 2016 09:04 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Please read and follow the forum guidelines, to enable us to help you:
OraFAQ Forum Guide
How to use {code} tags and make your code easier to read
Re: Oracle Dates query [message #648271 is a reply to message #648270] Fri, 19 February 2016 09:26 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>How to handle this in SQL?
You don't.
SQL statements must be known & static at compile time.
So either (ab)use EXECUTE IMMEDIATE or do it in PL/SQL
Re: Oracle Dates query [message #648273 is a reply to message #648265] Fri, 19 February 2016 10:01 Go to previous messageGo to next message
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 #648278 is a reply to message #648273] Fri, 19 February 2016 11:05 Go to previous messageGo to next message
anil_mk
Messages: 146
Registered: August 2006
Location: Bangalore, India
Senior Member

Hi Mike,

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.
Please suggest.
Below commented lines are copied from my SQLPLUS after running.
/*
SQL> alter session set nls_date_format = 'DD-MON-YYYY'
  2  /

Session altered.

SQL> drop table TMP_TEST
  2  /

Table dropped.

SQL> create table TMP_TEST (id number,DATES date)
  2  /

Table created.

SQL> insert into TMP_TEST values(1,'10-FEB-2016')
  2  /

1 row created.

SQL> insert into TMP_TEST values(2,'12-FEB-2016')
  2  /

1 row created.

SQL> insert into TMP_TEST values(3,'14-FEB-2016')
  2  /

1 row created.

SQL> insert into TMP_TEST values(4,'17-FEB-2016')
  2  /

1 row created.

SQL> commit
  2  /

Commit complete.

SQL>
*/

Thanks,
Anil MK
Re: Oracle Dates query [message #648282 is a reply to message #648278] Fri, 19 February 2016 11:08 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>General rule is if we pass value
post working code example showing how you "pass" value to SELECT statement
Re: Oracle Dates query [message #648283 is a reply to message #648278] Fri, 19 February 2016 11:16 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 #648286 is a reply to message #648285] Fri, 19 February 2016 11:38 Go to previous messageGo to next message
anil_mk
Messages: 146
Registered: August 2006
Location: Bangalore, India
Senior Member

Thanks Solomon for the query, it works fine.

Thanks,
Anil MK
Re: Oracle Dates query [message #648287 is a reply to message #648278] Fri, 19 February 2016 11:54 Go to previous message
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.

Previous Topic: Access variables from different script
Next Topic: Query
Goto Forum:
  


Current Time: Mon Jun 29 12:06:45 CDT 2026