Range by Day and Month [message #475978] |
Mon, 20 September 2010 06:51  |
meim
Messages: 35 Registered: July 2009
|
Member |
|
|
Hello,
I have table :TABLE_X and want to select some data locate into specific range of Day/Month. But so far i couldn't find out the way to.
For example, i want to select people born within specific range of date(range : sysdate to (sysdate+7months ahead) Year here should not be consider, only the day and month.
e.g. a range could be from today:Sept,20 to Apr,18.
so what i was trying is to select doing the following.
select TABLE_X_ID, TABLE_X_BIRTH_DATE
from TABLE_X
where to_date(TABLE_X_BIRTH_DATE, 'DD/MM')
between to_date (to_char(SYSDATE, 'DD/MM'), 'DD/MM')
and to_date (to_char(SYSDATE+210, 'DD/MM'), 'DD/MM')
first am not sure if BETWEEN & AND will work for this case, bt it was the most logical way i could think about to get such range.
Any help would be appreciated.
Meim
|
|
|
|
|
Re: Range by Day and Month [message #476005 is a reply to message #475978] |
Mon, 20 September 2010 10:13   |
pablolee
Messages: 2882 Registered: May 2007 Location: Scotland
|
Senior Member |
|
|
here is a partial solution. You will have to add and subtract 'bits' as appropriate, but the general idea is in there
WITH t AS (SELECT add_months(DATE'2010-10-10',-LEVEL) bdate, level idx
FROM dual
CONNECT BY LEVEL <=100)
SELECT bdate, idx, to_char(bdate,'mmdd-yyyy') x
FROM t
WHERE to_char(bdate,'mmdd') BETWEEN '0801' AND '1031'
order by 1
|
|
|
Re: Range by Day and Month [message #476015 is a reply to message #475978] |
Mon, 20 September 2010 12:56   |
joy_division
Messages: 4963 Registered: February 2005 Location: East Coast USA
|
Senior Member |
|
|
meim wrote on Mon, 20 September 2010 07:51
For example, i want to select people born within specific range of date(range : sysdate to (sysdate+7months ahead) Year here should not be consider, only the day and month.
Ok, is this another case where I do not understand the question, because why wouldn't
where date_column between sysdate and sysdate+add_months(sysdate,7)
not work?
|
|
|
|
Re: Range by Day and Month [message #476040 is a reply to message #476038] |
Mon, 20 September 2010 20:05   |
Solomon Yakobson
Messages: 3305 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
[quote title=Barbara Boehmer wrote on Mon, 20 September 2010 18:36[/quote]
As in most cases, February 29 messes things up. Your code will select people born on February 29 on leap year only:
SQL> create table emp1 as select * from emp
2 /
Table created.
SQL> update emp1
2 set hiredate = date '1984-02-29'
3 where ename = 'KING'
4 /
1 row updated.
SQL> SELECT ename,
2 hiredate
3 FROM emp1
4 WHERE TO_CHAR(hiredate,'DD/MM') IN (
5 SELECT TO_CHAR(SYSDATE + ROWNUM - 1,'DD/MM')
6 FROM DUAL
7 CONNECT BY LEVEL <= 211
8 )
9 /
ENAME HIREDATE
---------- ---------
SMITH 17-DEC-80
ALLEN 20-FEB-81
WARD 22-FEB-81
JONES 02-APR-81
MARTIN 28-SEP-81
JAMES 03-DEC-81
FORD 03-DEC-81
MILLER 23-JAN-82
8 rows selected.
SQL>
SY.
|
|
|
|
|
Re: Range by Day and Month [message #476045 is a reply to message #476005] |
Tue, 21 September 2010 00:48   |
meim
Messages: 35 Registered: July 2009
|
Member |
|
|
pablolee wrote on Mon, 20 September 2010 10:13here is a partial solution. You will have to add and subtract 'bits' as appropriate, but the general idea is in there
WITH t AS (SELECT add_months(DATE'2010-10-10',-LEVEL) bdate, level idx
FROM dual
CONNECT BY LEVEL <=100)
SELECT bdate, idx, to_char(bdate,'mmdd-yyyy') x
FROM t
WHERE to_char(bdate,'mmdd') BETWEEN '0801' AND '1031'
order by 1
Hi,
the problem is the interval of the Day and Month is not fixed in my case in which it should be [SYSDATE, ADD_MONTHS(sysdate, 7)] which mean it is not always that the beginning of the interval is lower that the end of the interval which makes BETWEEN & AND not working all the time. wht do you think ?
Thanks for your reply.
|
|
|
Re: Range by Day and Month [message #476046 is a reply to message #476045] |
Tue, 21 September 2010 01:03   |
pablolee
Messages: 2882 Registered: May 2007 Location: Scotland
|
Senior Member |
|
|
meim wrote on Tue, 21 September 2010 06:48
Hi,
the problem is the interval of the Day and Month is not fixed in my case in which it should be [SYSDATE, ADD_MONTHS(sysdate, 7)] which mean it is not always that the beginning of the interval is lower that the end of the interval which makes BETWEEN & AND not working all the time. wht do you think ?
Thanks for your reply.
Well, as I said, it is a partial solution. You need to apply some effort.
Quote:which mean it is not always that the beginning of the interval is lower that the end of the interval You have so far, given no information that implies this. So far you have said that the range is to be the 7 months proceeding the current date (year being taken out of the equation. Is this not the case?
Have you made any attempt to adapt my example to your needs?
CM: fixed a quote tag
[Updated on: Tue, 21 September 2010 06:08] by Moderator Report message to a moderator
|
|
|
Re: Range by Day and Month [message #476047 is a reply to message #476015] |
Tue, 21 September 2010 01:32   |
meim
Messages: 35 Registered: July 2009
|
Member |
|
|
joy_division wrote on Mon, 20 September 2010 12:56meim wrote on Mon, 20 September 2010 07:51
For example, i want to select people born within specific range of date(range : sysdate to (sysdate+7months ahead) Year here should not be consider, only the day and month.
Ok, is this another case where I do not understand the question, because why wouldn't
where date_column between sysdate and sysdate+add_months(sysdate,7)
not work?
Hi,
I can't say this directly as the date_colum is Birth Date which mean year wont match the year of the sysdate. plus, the problem i mentioned in my previous reply about using BETWEEN & AND.
Thanks for your reply
|
|
|
|
Re: Range by Day and Month [message #476096 is a reply to message #476050] |
Tue, 21 September 2010 06:06  |
Solomon Yakobson
Messages: 3305 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
meim wrote on Tue, 21 September 2010 03:00Hi I tried your solution and it do the work.
First of all, I did not offer any solutions. I just pointed out Barbara's solution does not deal with Feb 29 properly. Below is my suggestion. It assumes humans do not live more than 150 years (with some cushion for future medical break-throughs ). Table emp1 is same as emp with King's hiredate changed to Feb 29. Solution simply generates "anniversary dates" and selects people who's "anniversary dates" are within 7 months from today:
SELECT ename,
hiredate
from emp1,
(
select (level - 1) * 12 offset
from dual
connect by level <= 151
)
where add_months(hiredate,offset) >= trunc(sysdate)
and add_months(hiredate,offset) < add_months(trunc(sysdate),7)
/
ENAME HIREDATE
---------- ---------
SCOTT 19-APR-87
KING 29-FEB-84
MARTIN 28-SEP-81
JAMES 03-DEC-81
FORD 03-DEC-81
MILLER 23-JAN-82
SMITH 17-DEC-80
ALLEN 20-FEB-81
WARD 22-FEB-81
JONES 02-APR-81
10 rows selected.
SQL>
SY.
P.S. You might want to add DISTINCT to the above query in case search interval spans one year
[Updated on: Tue, 21 September 2010 06:09] Report message to a moderator
|
|
|