this is right
select to_date(substr('2002-49',1
You can write any week in format 'yyyy-iw' instead of '2002-49'
good luck,
Kanan
Main Topics
Browse All TopicsThe Input format will be in week number and we need to find the first day of the week in Oracle's default date format.
I tried to use, to_date('2002-49','YYYY-IW
Gopi.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Comments from gmyers and Kannan worked but not for all situations. For Example I ran the script for Week '2002-01'. I got the First Day of week as '01-Jan-02' and Last Day of week as '31-Dec-02'. This is not correct. The correct result is '31-Dec-01' is First Day and '06-Jan-02' is the last day.
I have changed the logic sent by gmyers a little bit to get correct result for any week of any year. This is a bit complex but it gives the correct result.
Thank you guys for giving me the startup.
Gopi.
select min_date, to_char(min_date,'DAY') DAY from
(select to_date(substr(:Week_no,1,
from all_tables
where rownum <= decode(mod(to_number(subst
UNION
-- To Fetch Dates of previous year falling into current year's 1st week
select to_date(substr(:Week_no,1,
decode(mod(to_number(subst
from all_tables
where rownum <= 7
UNION
-- To Fetch Dates of Next year falling into current year's last week
select to_date(substr(:Week_no,1,
from all_tables
where rownum <= 7
)
where to_char(min_date,'IYYY-IW'
I run my code and get:
select to_date(substr('2002-01',1
1 row(s) selected
FIRST_DAY
--------------------
31-Dec-2001
select to_date(substr('2002-01',1
1 row(s) selected
LAST_DAY
--------------------
6-Jan-2002
good luck
Hi Kannan,
I ran your code for '2002-01' it did give correct answer, but not for '2003-01'
You code gave 12/31/02 as First Day and 01/06/03 as Last day. This is not the correct answer.
The correct values for '2003-01' are
First Date Day Name
---------- --------
12/30/02 Monday
Last Date Day Name
--------- --------
01/05/03 Sunday
I run your code for '2001-01' it did not give the correct result.
Thank you
Gopi.
try this:
select trunc(to_date(substr('2003
1 row(s) selected
FIRST_DAY
--------------------
30-Dec-2002
select trunc(to_date(substr('2003
1 row(s) selected
LAST_DAY
--------------------
5-Jan-2003
select trunc(to_date(substr('2002
1 row(s) selected
FIRST_DAY
--------------------
31-Dec-2001
select trunc(to_date(substr('2002
1 row(s) selected
LAST_DAY
--------------------
6-Jan-2002
good luck,
Kanan.
Your Query worked for most of the cases, but it failed in one case.
For Example I ran it for week 2000-52. It gave First day as 18-Dec-2000 and Last Day as 24-Dec-2000. This is correct.
Next I ran for week 2001-01, the results are 1-Jan-2001 and 7-Jan-2001. This is correct.
What happened to the days between 25-Dec-2000 and 31-Dec-2000, One whole week is missing. Actually there are 53 weeks in 2000. If I run for week 2002-53, it fails.
Thank you
Gopi.
try this
SELECT TRUNC(DECODE(ww, 53, TO_DATE(yy || '3112', 'yyyyddmm'), TO_DATE(yy || '-' || TO_CHAR(ww * 7), 'yyyy-ddd')), 'd') - 6 first_day
FROM (SELECT SUBSTR('2000-53', 1, 4) yy, TO_NUMBER(SUBSTR('2000-53'
FROM DUAL)
1 row(s) selected
FIRST_DAY
-----------------------
25-Dec-2000
SELECT TRUNC(DECODE(ww, 53, TO_DATE(yy || '3112', 'yyyyddmm'), TO_DATE(yy || '-' || TO_CHAR(ww * 7), 'yyyy-ddd')), 'd') last_day
FROM (SELECT SUBSTR('2000-53', 1, 4) yy, TO_NUMBER(SUBSTR('2000-53'
FROM DUAL)
1 row(s) selected
LAST_DAY
-----------------------
31-Dec-2000
The last piece of code works only for week 53, if you try to run for week 52 it give the same result as week 53.
One code should work for all cases. It is not possible to run diffrent codes for different conditions.
I have created a database function as per the logic I sent earlier. It works fine for all cases.
Thank you.
Gopi.
You did something wrong, this is corect code:
SELECT TRUNC(DECODE(ww, 53, TO_DATE(yy || '3112', 'yyyyddmm'), TO_DATE(yy || '-' || TO_CHAR(ww * 7), 'yyyy-ddd')), 'd') last_day
FROM (SELECT SUBSTR('2000-52', 1, 4) yy, TO_NUMBER(SUBSTR('2000-52'
FROM DUAL)
1 row(s) selected
LAST_DAY
-----------------------
24-Dec-2000
Business Accounts
Answer for Membership
by: gmyersPosted on 2002-12-05 at 14:23:48ID: 7539289
This may help.
) + rownum) v_date
select min(v_date) from
(select (to_date('200201','YYYYMM'
from all_tables
where rownum < 370)
where to_char(v_date,'YYYY-IW') = '2002-49'