0

I was looking up a way to solve the following:

Display the Week (eg: 1-52), start and end date of that week and a count of something.

The week, was figured to be something like: to_Char(, 'WW')

but i dont know how to get the first and last date of those weeks.

How do i do that with SqlPlus?

Edit:

Given: a table that contains a column of dates.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Fallenreaper
  • 10,222
  • 12
  • 66
  • 129
  • Given you mention SQLPlus, can we assume this is Oracle? – Joe Feb 21 '13 at 03:47
  • yes you can. my coworker has me referencing it as SQLPLus but i just call it oracle to most. – Fallenreaper Feb 21 '13 at 03:48
  • An answer to a closely related question, [How do I calculate the week number given a date?](http://stackoverflow.com/questions/274861/how-do-i-calculate-the-week-number-given-a-date/275024#275024), may help you with some of the theoretical background. In the interim, look up the ISO 8601 week number format specifier. The first and last day of the week are Monday and Sunday in the ISO 8601 calendar. If you get the 'day of the week' as a number D (in the range 1-7), then you can subtract (D-1) days from the date to get the first day and add 6 days to get the last day of the week. – Jonathan Leffler Feb 21 '13 at 03:53

2 Answers2

3

Try something like this:

SELECT to_char(date_col, 'iw') w, trunc(date_col, 'd') st, trunc(date_col, 'd')+6 et
FROM your_table

Here is a sqlfiddle demo

References:

Andriy M
  • 76,112
  • 17
  • 94
  • 154
A.B.Cade
  • 16,735
  • 1
  • 37
  • 53
1

For the first date of the week, try next_day(dateColumn-8, 'Monday')

For the last date of the week, try next_day(dateColumn-1, 'Sunday')

hd1
  • 33,938
  • 5
  • 80
  • 91