Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get week-wise data in Oracle

Tags:

sql

oracle

I have made a matrix report in which need to display column dynamically based upon the selection parameter. I have a selection paramter of date.

If I select Date on selection paramater as "03/01/2010" i.e. 1st March 2010then it should display like 1st March - 7th March

like image 573
user1099310 Avatar asked Feb 22 '23 16:02

user1099310


1 Answers

It depends what you're after. If you're after the next 7 days then:

select * 
  from my_table
 where date_col between :my_date and :my_date + 7

If you want say Monday to Sunday then use the next_day function:

select *
  from my_table
 where date_col between next_day(:my_date, 'Monday') - 7 
                    and next_day(:my_date, 'Monday')

Both where :my_date is the date your passing in.

If you're not passing in a date but a string then the first one would become, using the to_date function:

select *
  from my_table
 where date_col between to_date(:my_date,'dd/mm/yyy') + 7
                    and to_date(:my_date,'dd/mm/yyy')

and you could do something similar for the second. If you have to use to_date then date_col should have a function-based index on to_date(date_col,'dd/mm/yyyy') or if you're going to be converting it differently then that way.

like image 181
Ben Avatar answered Mar 05 '23 07:03

Ben