SQL Query to get Date Range Values

Hi,
The database is Oracle11i.
I am looking for a way to generate list of dates from a fixed date in the past (could be hardcoded) to current day (sysdate).
That is, if the fixed date is 19 June 2011 and assuming that today is 24 June 2011 the SQL should be able to generate the
following:-
19-June-2011
20-June-2011
21-June-2011
22-June-2011
23-June-2011
24-June-2011
And the constraint is that I can't make any change to the database in question. I can only fire an SQL query (SELECT). No
usage of time dimension kind of approach (time dimension is not available here) and no procedures, PL/SQL etc. Is there any way?
Thanks

Jaimeen Shah wrote:
Hi,
The database is Oracle11i.
I am looking for a way to generate list of dates from a fixed date in the past (could be hardcoded) to current day (sysdate).
That is, if the fixed date is 19 June 2011 and assuming that today is 24 June 2011 the SQL should be able to generate the
following:-
19-June-2011
20-June-2011
21-June-2011
22-June-2011
23-June-2011
24-June-2011
And the constraint is that I can't make any change to the database in question. I can only fire an SQL query (SELECT). No
usage of time dimension kind of approach (time dimension is not available here) and no procedures, PL/SQL etc. Is there any way?
Thanks
SQL> def date_start = '13/11/2010'
SQL> def date_end   = '22/11/2010'
SQL> with
  2    data as (
  3      select to_date('&date_start', 'DD/MM/YYYY') date1,
  4             to_date('&date_end',   'DD/MM/YYYY') date2
  5      from dual
  6    )
  7  select to_char(date1+level-1, 'DD/MM/YYYY') the_date
  8  from data
  9  connect by level <= date2-date1+1
10  /
THE_DATE
13/11/2010
14/11/2010
15/11/2010
16/11/2010
17/11/2010
18/11/2010
19/11/2010
20/11/2010
21/11/2010
22/11/2010

Similar Messages

  • Oracle SQl query to find date range based on another cloumn value

    Hi Folks,
    I want to extract records for the employees who have consecutive vacation/leave from a table. If an emp has vacation of 3 days (MON-WED), the table contains 3 distinct records for him,
    e.g. My table contains records as shown below.
    EmpName Paycode ApplyDate Amt. of Hrs
    emp1 vacation 5/1/2010 8
    emp1 vacation 5/2/2010 8
    emp1 vacation 5/3/2010 8
    I am trying to get the output like this...
    Emp Name Paycode Leave Start Date Leave End Date TotalHrs
    emp1 vacation 5/1/2010 5/3/2010 24
    Note: If the smae emp has sets of vacation in another month, that should come as a separate record with start date and end date(last date of vacation for that set).
    I have a query which does not return any rows. Any help to repair this query or any better one would be of great help.
    ==================================================================
    WITH vpt AS (
    select personnum as empname, paycodename as paycode, applydate, timeinseconds/3600 as numhours from VP_TOTALS
    where applydate between to_date('05/01/2010','MM/DD/YYYY') AND to_date('12/31/2010','MM/DD/YYYY')
    AND paycodename in ('US-Vacation','US-Bereavement','US-Sick','US-Jury Duty')
    select
    empname,
    paycode,
    min(applydate) as startdate,
    max(applydate) as enddate,
    sum(numhours) as totalhours
    from (
    select
    empname,
    paycode,
    applydate,
    numhours,
    -- number the blocks sequentially
    sum(is_block_start) over (partition by empname, paycode order by applydate) as block_num
    from (
    select
    empname,
    paycode,
    applydate,
    numhours,
    -- Mark the start of each block
    case
    when applydate = prev_applydate + 1 then 0 else 1 end as is_block_start
    from (
    select
    empname,
    paycode,
    applydate,
    numhours,
    lag (applydate) over (partition by empname, paycode order by applydate) prev_applydate
    from vpt
    group by empname, paycode, block_num
    ===================================================================
    Thanks,
    Maha

    Hi Dear,
    Can I do reverse I mean I can get output as your question from your output as below:
    I have this table
    FID      STARTD ATE END DATE
    1 01-MAY-10 03-MAY-10
    1 09-MAY-10 11-MAY-10
    1 03-JUN-10 04-JUN-10
    2 03-JUN-10 04-JUN-10
    2 04-AUG-10 04-AUG-10
    2 06-AUG-10 06-AUG-10
    I want like this.
    FID FDATE
    1 01-MAY-10
    1 02-MAY-10
    1 03-MAY-10
    1 09-MAY-10
    1 10-MAY-10
    1 11-MAY-10
    1 03-JUN-10
    1 04-JUN-10
    2 03-JUN-10
    2 04-JUN-10
    2 04-AUG-10
    2 06-AUG-10
    And:
    How can i get date wise entry from Joining date to relieving date like..
    FID      START DATE END DATE
    1 01-MAY-10 03-MAY-12
    1 09-MAY-10 11-MAY-11
    2 04-AUG-10 04-AUG-11
    I want like this.
    FID FDATE
    1 01-MAY-10
    1 03-MAY-10
    1 04-MAY-10
    1 05-MAY-10
    1 16-MAY-10
    1 17-MAY-10
    1 08-May-10
    1 09-May-10
    1 03-May-12
    Can you please help me.
    Thanks,
    Edited by: 978452 on Dec 24, 2012 12:02 AM

  • Sql Query for getting data between two dates

    Dear Sir,
    I have one problem, i have date stored in varchar datatype of (X) table in mysql as it was not supporting the date format of (dd/mm/yyyy) so know the problem is,
    When i going to get the data between the the two dates it is giving the un reliable answers such that
    when i am asking the query as data between 1st April to 31st December 2009 it is showing the data from the 2008 .
    is there any way to solve this problem,please help this is an urgent requirement, your replay will be helpful to me.
    With Regards
    Ramakrishna Y

    You either have a SQL problem and this is the incorrect place to ask.
    Or you have a Java problem in code we cannot see. When posting code use the code tags. Click the CODE button and paste your code between the tags that appear.

  • Query to get date range in Week

    Hello all,
    I wanted the get the results in terms of week like, if sysdate is 26/7/2007 Then
    I have to get previous 7 weeks including the week where sysdate exists.
    Results should come in this manner
    23rd to 27th 1 week
    16 - 20th July 2007 as 2nd week like this till 7 weeks
    I have a query but it is not giving in the above manner
    So please anybody can make a query as mentioned above.
    Query:
    SELECT TO_DATE(TO_CHAR(SYSDATE,'DD-MON-YYYY')) + ( ROWNUM - 6)+(ROWNUM+2)D,TO_DATE(TO_CHAR(SYSDATE,'DD-MON-YYYY')) + ( ROWNUM - 6)+(ROWNUM+2) R FROM DUAL CONNECT BY LEVEL < 6

    What do you want?
    -- A week
    select sysdate, sysdate+7 from dual;
    --A week from monday to sunday
    select nexT_DAY(SYSDATE, 'MONDAY'), NEXT_day(sysdate-7, 'SUNDAY') FROM DUAL
    BE CAREFUL MONDAY AND SUNDAY IF YOUR DDBB IS IN ENGLISH
    select
    nexT_DAY(SYSDATE, 'MONDAY'), NEXT_day(sysdate-7, 'SUNDAY'), TO_char(sysdate,'d'),
    (sysdate -TO_char(sysdate,'d')) as firstone,
    (sysdate + 7-TO_char(sysdate,'d') )as lastone
    FROM DUAL
    Message was edited by:
    cth

  • SQL query to get dates between two dates

    Hi,
    We have one table with start date and end date...Now i need to get all dates between start date and end date...
    Table looks like below..
    Create table date_table (start_date date,end_date date);
    Data in the table will be like below:
    start_date            end_date
    01-Jan-2013       04-Jan-2013
    01-Feb-2013       03-Feb-2013
    01-May-2013        03-May-2013
    I want a result as below..
    holiday_dates
    01-Jan-2013
    02-Jan-2013
    03-Jan-2013
    04-Jan-2013
    01-Feb-2013
    02-Feb-2013
    03-Feb-2013
    01-May-2013
    02-May-2013
    03-May-2013
    Can any one helps..?

    select * from date_table;
    START_DAT END_DATE
    01-JAN-13 04-JAN-13
    01-FEB-13 07-FEB-13
    07-MAR-13 12-MAR-13
    select start_date-1+level output from
    date_table connect by level<=end_date-start_date+1
    and prior start_date=start_date and prior sys_guid() is not null
    OUTPUT
    01-JAN-13
    02-JAN-13
    03-JAN-13
    04-JAN-13
    01-FEB-13
    02-FEB-13
    03-FEB-13
    04-FEB-13
    05-FEB-13
    06-FEB-13
    07-FEB-13
    07-MAR-13
    08-MAR-13
    09-MAR-13
    10-MAR-13
    11-MAR-13
    12-MAR-13

  • SQL query to get the Datetime 06 hours prior to the table Datetime value

    Hi Experts,
                    I'm just trying to create a SQL query to get the Datetime which should be 06 hours prior to my Table column value(Executiontime),
    Eg: my Executiontime(column) value is 07:00AM means, this query should fetch the detail of first VMName from table at 01:00AM, 
    SQL Table Name: TestTable
    Columns: VMName(varchar),status(varchar) Executiontime(Datetime)
    SQL Query : Select Top 1 VMName from
    TestTable where convert(date,Exeutiontime)=convert(date,getdate()) and
    status='0' and ExecutionTime > dateadd(hour,6,getdate())
    Request someone to alter this Query to my requirement or give me the new one.
    Regards,
    Sundar
    Sundar

    Hi All,
            Thanks for your Prompt response. I tried the below queries, but still I don't have any luck. Actually the queries are returning the value before the condition met (say when the time difference is more than 06 hours). I want the
    query to return exactly @ 06 hour difference or less than 06 hours,
    Query 01: Select Top 1 VMName from TestTable where
    convert(date,Exeutiontime)=convert(date,getdate())
    and status='0'
    and ExecutionTime >
    dateadd(hour,-6,getdate())
    Query 02: Select
    Top 1 VMName from TestTable where
    status='0'
    and ExecutionTime >
    dateadd(hour,-6,getdate())
    Query 03: Select
    Top 1 VMName from TestTable where status='0'
    and ExecutionTime >
    dateadd(hour,-6,ExecutionTime)
              Can someone point out the mistake please.
    Regards,
    Sundar
    Sundar

  • Need a sql query to get multiple dates in rows

    Hi All,
    i need a query to get dates for last 7 days and each dates should be in one row...
    but select sysdate from dual..gives one row...
    Expexcted Output
    Dates:
    01-oct-2013
    30-sep-2013
    29-sep-2013
    28-sep-2013
    27-sep-2013
    26-sep-2013

    Hi,
    Do you mean that you want all 7 dates together on 1 row?
    Here's one way:
    SELECT LISTAGG ( TO_CHAR ( SYSDATE + 1 - LEVEL
                              , 'DD-Mon-YYYY'
                    ) WITHIN GROUP (ORDER BY LEVEL)    AS txt
    FROM    dual
    CONNECT BY LEVEL <= 7
    This is an example of String Aggregation, that is, taking a column on multiple rows, and concatenating all the values (however many htere happen to be) into 1 big string column 1 row.
    Like everything else, exactly how to do it depends on your Oracle version.
    For more on String Aggregation, including differetn techniques for different versions, see http://www.oracle-base.com/articles/10g/StringAggregationTechniques.php
    Message was edited by: FrankKulash
    Sorry, I mis-read the question.

  • Need help in SQL query to get only distinct records

    Hi all.
    I am new to oracle SQL and i want to write a query to get only distinct values from 2 tables.
    for example i hade 2 tables table1(employee_id,employee_no,grade) and another table2 with same structure.
    for example data in table1 is
    Employeeid employeeno Grade
    1 101 A
    1 101 B
    1 101 E
    1 101 D
    and data in table 2 is
    Employeeid employeeno Grade
    1 101 A
    1 101 B
    1 101 E
    i want to display only the fourth row like the data that doesnt exist in table2 i,e (1 101 D)
    I had too much records in both tables and i jst want to see the records that is in table 1 not in table 2 only.
    employeeid and employeeno are the same in both tables
    I hope u understand my query. I user left outer join and right outer join bt it displays all the results that and matching and unmatching. I want jst unmatched records.
    Any help is highly Appreciated.
    Thanx in Advance.
    I am using oracle 10G on windows

    Try MINUS:
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries004.htm#i2054381
    SQL> -- generating sample data:
    SQL> with t1 as (
      2  select 1 employeeid, 101 employeeno, 'A' grade from dual union
      3  select 1, 101, 'B' from dual union
      4  select 1, 101, 'E' from dual union
      5  select 1, 101, 'D' from dual
      6  )
      7  ,  t2 as (
      8  select 1 employeeid, 101 employeeno, 'A' grade from dual union
      9  select 1, 101, 'B' from dual union
    10  select 1, 101, 'E' from dual
    11  )
    12  --
    13  -- actual query:
    14  --
    15  select employeeid
    16  ,      employeeno
    17  ,      grade
    18  from   t1
    19  minus
    20  select employeeid
    21  ,      employeeno
    22  ,      grade
    23  from   t2;
    EMPLOYEEID EMPLOYEENO G
             1        101 D
    1 row selected.

  • How to run recordset SQL query in FORM DATA event

    How can I run recordset SQL query in FORM DATA event upon clicking on Add button in the document?

    Hi Slamet,
    When you receive a form data event you have a class containing inside it a ObjectKeys xml info regarding the object added/modified,...
    With the objects keys you can then use the DI API method called GetByKeys to obtain the DI Object.
    There is some information about it in the SDK Help file, mainly in the "FormDataEvent" class definition:
    <i>The event provides the unique ID (BusinessObjectInfo.ObjectKey) of the modified business object. You can use the value of this property as an input parameter in the DI API DataBrowser.GetByKeys method to get a DI object.</i>
    Pay attention you don't have this information in the Before=True event of the Add as it is not yet in the database.
    Hope it helps
    Trinidad.

  • SQL query to get transaction detail from DEFERRED_TRAN_ID

    Hi,
    I'm using Oracle Advance Replication and get the transaction detail from Enterprise Manager Console.
    So instead of using the console.
    Is there a way any SQL query which get transaction details like SQL query fired, old and new column value etc from DEFERRED_TRAN_ID.
    thanks

    quote:
    Originally posted by:
    lucapac
    I have two tables: tblWorkers and tblSkills. tblWorkers has a
    column, Skills, which is populated from a multiple-checkbox form
    field with one or more skill_IDs from tblSkills, so each
    tblWorkers.Skills consists of a list of one or more comma-delimited
    values. For any Skill_ID, I need to generate a listing of all
    Workers with the corresponding skill, so I have tried to do
    something along the lines of SELECT WorkerName FROM tblWorkers
    WHERE Skills IN (Skills, #FORM.Skill_ID#) ... or WHERE Skills IN
    (ListFind(Skills, #FORM.Skill_ID#)) ... etc. ??? My results (once I
    got data type mismatches out of the way) return all Workers, not
    just those with the desired Skill. There must be an easy way to do
    this ... How do people with a bit more CF/SQL experience than I
    have do this???
    As Kronin implied, we normalize our databases. If you don't
    understand that answer, the book "Database Design for Mere Mortals"
    is often mentioned on this forum.

  • How will write SQL query to fetch data from  each Sub-partition..

    Hi All,
    Anyone does have any idea about How to write SQL query to fetch data from Sub-partition.
    Actually i have one table having composite paritition(Range+list)
    Now if i want to fetch data from main partition(Range) the query will be
    SELECT * FROM emp PARTITION(q1_2005);
    Now i want to fetch data at sub-partition level(List) .But i am not able to get any SQL query for that.
    Pls help me to sort out.
    Thanks in Advance.
    Anwar

    SELECT * FROM emp SUBPARTITION(sp1);

  • Single query to get data from different databases

    i need to capture certain fields from certain tables in database 1 and certain fields from certain tables in database 2 into one file using a single SQL statement.
    i tried searching on the net
    i found that dblinks can help
    but iam not sure if ill be able to create dblinks in my situation which is:
    i need to get data from oracle to be copied to mysql
    this is not a replication acitivity, but i need certain fields from one database and certain from the other
    so what iwas thinking is, if i use an sql query to get all the fields (i need around 40) from the different oracle databases and create a singlefile with one insert per select, i can then read that file into mysql
    instead of creating multiple sql queries for each table and creating separate files and eventually separate tables in mysql.
    can anyone help me here?
    or maybe suggest another approach.
    thanks

    Hi,
    I think dblink is the only option available to get data from different databases. It will work for your case too.
    CREATE DATABASE LINK db_link CONNECT TO user_name IDENTIFIED BY  password USING 'instance_name'you must have the system privilege 'create database link' to create db links. This way you can get the required data and put it in a table in oracle. But i dont know how to put this data from oracle table to Mysql.
    HTH
    Muneer

  • SQL query to get a list of relations between workitems

    How can I create a SQL query to get a list of all problems with related changes?  And all problems with related incidents?
    I have tried to join the tables RelationshipTypeDim with ProblemDimKey and ChangeRequestDim, but the results are not correct.

    The relationships in the data warehouse can be kind of tricky. The relationships are contained in the WorkItemRelatesToWorkItemFactvw table. This table lists the related items by their WorkItemDimKey, so you cannot reference directly from the ChangeRequestDimvw
    or ProblemDimvw. You will need to reference the WorkItemDimvw to get the WorkItemDimKey for each entry.
    The query below will get all of the related work items from the Change Request class. The way the joins work is ChangeRequestDimvw gets the list of change requests. Then inner joins WorkItemDimvw to get the WorkItemDimKey for each CR. Then inner joins WorkItemRelatesToWorkItemFactvw
    to get all of the CRs with related work items. Then inner joins the WorkItemDimvw again to get the ID of the related work item. 
    Now the tricky part is it appears that these relationship are set based on which item  that created the relationship. So you need to union a second query that reverse the relationship on the WorkItemRelatesToWorkItemFactvw. 
    This query should give you a good start on getting the related work items. You can filter it down from here if you only want to include problems.
    SELECT C.ID, WIWI.ID
    FROM dbo.ChangeRequestDimvw C
    INNER JOIN dbo.WorkItemDimvw WI ON
    WI.EntityDimKey = C.EntityDimKey
    INNER JOIN dbo.WorkItemRelatesToWorkItemFactvw AS WIRWI ON
    WIRWI.WorkItemDimKey = WI.WorkItemDimKey
    INNER JOIN dbo.WorkItemDimvw AS WIWI ON
    WIWI.WorkItemDimKey = WIRWI.WorkItemRelatesToWorkItem_WorkItemDimKey
    union
    SELECT C.ID, WIWI.ID
    FROM dbo.ChangeRequestDimvw C
    INNER JOIN dbo.WorkItemDimvw WI ON
    WI.EntityDimKey = C.EntityDimKey
    INNER JOIN dbo.WorkItemRelatesToWorkItemFactvw AS WIRWI ON
    WIRWI.WorkItemRelatesToWorkItem_WorkItemDimKey = WI.WorkItemDimKey
    INNER JOIN dbo.WorkItemDimvw AS WIWI ON
    WIWI.WorkItemDimKey = WIRWI.WorkItemDimKey
    Order by C.ID
    Matthew Dowst |
    Blog | Twitter

  • Sql query to get distinct count

    Hi
    I use SQL Server Management Studio
    can I have a sql query to get count as shown below against  each month column and name column to get distinct count.
    for example if there is two rows with the same date period and same name then the count should be one in first row and zero in the next row of the same data.
    Table Name: Table1
    Column: Month, Name
    Month
    Name
    Count
    12/1/2012 0:00
    AK
    1
    12/1/2012 0:00
    AK
    0
    12/1/2012 0:00
    AB
    1
    1/1/2013 0:00
    AK
    1
    1/1/2013 0:00
    AK
    0
    1/1/2013 0:00
    AB
    1
    3/1/2013 0:00
    AA
    1
    3/1/2013 0:00
    AK
    1
    3/1/2013 0:00
    AK
    0
    6/1/2013 0:00
    AA
    1
    6/1/2013 0:00
    AK
    1
    6/1/2013 0:00
    AK
    0
    9/1/2013 0:00
    AA
    1
    9/1/2013 0:00
    AK
    1
    9/13/2013 0:00
    AK
    1
    10/1/2013 0:00
    AA
    1
    10/1/2013 0:00
    AK
    1
    10/1/2013 0:00
    AK
    0

    Hi,
    Thanks for the query but this query gives the total count like shown below
    if see the second row in the below table AK for 2012-12-1 gives total count as 2 but need the query to show the first row as 1 and there after 0
    query result
    Month name cnt
    2012-12-01 00:00:00.000 AB 1
    2012-12-01 00:00:00.000 AK 2
    2012-12-01 00:00:00.000 AK 2
    2013-01-01 00:00:00.000 AB 1
    2013-01-01 00:00:00.000 AK 2
    2013-01-01 00:00:00.000 AK 2
    2013-03-01 00:00:00.000 AA 1
    2013-03-01 00:00:00.000 AK 2
    2013-03-01 00:00:00.000 AK 2
    2013-06-01 00:00:00.000 AA 1
    2013-06-01 00:00:00.000 AK 2
    2013-06-01 00:00:00.000 AK 2
    2013-09-01 00:00:00.000 AA 1
    2013-09-01 00:00:00.000 AK 1
    2013-09-13 00:00:00.000 AK 1
    2013-10-01 00:00:00.000 AA 1
    2013-10-01 00:00:00.000 AK 2
    2013-10-01 00:00:00.000 AK 2

  • SQL query to get last 6 months records neglect the current month

    Hi All;
    I need help with 
    sql query to get last 6 months records neglect the current month
    Any help much appreciated
    Thanks
    Pradnya07

    SELECT <> FROM tbl WHERE dt >=dateadd(month,-6,GETDATE())
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

Maybe you are looking for

  • How to replace an MDB with a servlet

    I got a situation to replace an MDB with a servlet,How can I do that,more appreciated if u send me some sample code.

  • Merge in HDR Pro Problem

    I select images in Lightroom 5 and then select Merge in HDR Pro.  The images go  to Photoshop (CC), but when the HDR dialog opens, the imaage area is black.  I also have PS CS6 and it works correctly there.  Is this a bug in CC?

  • Absolute URLs being changed to relative URLs

    (Using DreamWeaver CS3) DreamWeaver keeps changing the Absolute URLs for this site's Flash menu into relative links which crashes the menu. Does anyone know how to stop this? The Flash menu source code is hard-coded into the DW Template file within O

  • I can make individuals match to target areas (as there are tutorials for that) but i want each one

    hi in drag and drop  I can make individuals match to target areas (as there are tutorials for that) but i want each one to be able to slide in to any target area (and someday i want to create a solve button which will then place each element in the c

  • Oracle apps technical consultant

    what is database wheather it is s/w or h/w ? what is oracle database wheather it is s/w or h/w ? if we install oracle 10g where it can be stored in harddisk or somewhere else ? oracle occupy large amount of disk space why ?