Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

number of week in year - oracle syntax

Posted on 2011-10-04
3
Medium Priority
?
390 Views
Last Modified: 2012-05-12
I've got some sql code which lists all the days within the last 4 weeks of the report run date.
I now need to add another field in which calculates the week number (in the year). The to_char(d, 'ww') function doesnt quite fit my requirements - i need a week to run from Mon-Sun, not Sat-Fri.
Fig1. is the current output (using the code below), Fig2. is the desired output.
Any help with the code is appreciated output.xls
SELECT d asm_date, TO_CHAR(d, 'Day') day_of_week, to_char(d, 'ww') week_no
  FROM (SELECT NEXT_DAY(TRUNC(SYSDATE - 1), 'Sunday') - LEVEL + 1 d
          FROM DUAL
        CONNECT BY LEVEL <= 28)
ORDER BY 1

Open in new window

0
Comment
Question by:tonMachine100
  • 2
3 Comments
 
LVL 7

Expert Comment

by:Jacobfw
ID: 36909441
Like this (just added 2 days for the week calculation):

SELECT d asm_date, TO_CHAR(d, 'Day') day_of_week, to_char(d-2, 'ww') week_no
  FROM (SELECT NEXT_DAY(TRUNC(SYSDATE - 1), 'Sunday') - LEVEL + 1 d
          FROM DUAL
        CONNECT BY LEVEL <= 28)
ORDER BY 1
0
 
LVL 7

Expert Comment

by:Jacobfw
ID: 36909452
Gets a little funky around the yearend though.  What do you want for the last week/first week of the year?
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 2000 total points
ID: 36909804
are you counting whole weeks or partial weeks too?

for instance 2011 starts on a Saturday.  Is week 1 of 2011 a 2-day week?  Or, do you start counting weeks from the first full week of the year?

Are you looking for iso weeks?   if so, try iw format


SELECT d asm_date, TO_CHAR(d, 'Day') day_of_week, to_char(d, 'iw') week_no
  FROM (SELECT NEXT_DAY(TRUNC(SYSDATE - 1), 'Sunday') - LEVEL + 1 d
          FROM DUAL
        CONNECT BY LEVEL <= 28)
ORDER BY 1
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Screencast - Getting to Know the Pipeline
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question