Sql query to find the scheduled date

I schedule a job for last Thursday of every month. Based on my current date and time I need the date of my next scheduled run of the job...
(i.e) If today is 28-march-2012 (Wednesday) my query should return 3/29/12(which is the last thursday of the month)
If today is 30-march-2012 (Friday) my query should return 4/26/12 (which is the last thursday of next month)
If today is 29-march-2012 and time is 11:00 AM and i have scheduled a job to run at 12:00 PM of last thursday of every month then my query should return 3/29/12
If today is 29-march-2012 and time is 1:00 PM then my query should return 4/26/12

924163 wrote:
I schedule a job for last Thursday of every month. Based on my current date and time I need the date of my next scheduled run of the job...
(i.e) If today is 28-march-2012 (Wednesday) my query should return 3/29/12(which is the last thursday of the month)
If today is 30-march-2012 (Friday) my query should return 4/26/12 (which is the last thursday of next month)
If today is 29-march-2012 and time is 11:00 AM and i have scheduled a job to run at 12:00 PM of last thursday of every month then my query should return 3/29/12
If today is 29-march-2012 and time is 1:00 PM then my query should return 4/26/12may be
SQL> ed
Wrote file afiedt.buf
  1  with tmp as
  2  (
  3  select to_date('28-march-2012','DD-month-YYYY') dt from dual union all
  4  select to_date('29-march-2012 11:00:00 AM','DD-month-YYYY HH:MI:SS AM') dt from dual union all
  5  select to_date('29-march-2012 1:00:00 PM','DD-month-YYYY HH:MI:SS AM') dt from dual union all
  6  select to_date('29-march-2012 12:00:00 PM','DD-month-YYYY HH:MI:SS AM') dt from dual union all
  7  select to_date('29-march-2012 11:59:00 AM','DD-month-YYYY HH:MI:SS AM') dt from dual union all
  8  select to_date('30-march,2012','DD-month-YYYY') dt from dual
  9  )
10  select dt,
11         case
12           when Next_day(Add_months(Trunc(dt, 'MM'), 1) - 7, 'thursday') + ( 12 /
13                24 ) >
14                dt then Next_day(Add_months(Trunc(dt, 'MM'), 1) - 7, 'thursday')
15           else Next_day(Add_months(Trunc(dt, 'MM'), 2) - 7, 'thursday')
16         end new_dat
17  from   tmp
18* order  by dt
SQL> /
DT                      NEW_DAT
28-Mar-2012 12:00:00 AM 29-Mar-2012 12:00:00 AM
29-Mar-2012 11:00:00 AM 29-Mar-2012 12:00:00 AM
29-Mar-2012 11:59:00 AM 29-Mar-2012 12:00:00 AM
29-Mar-2012 12:00:00 PM 26-Apr-2012 12:00:00 AM
29-Mar-2012 01:00:00 PM 26-Apr-2012 12:00:00 AM
30-Mar-2012 12:00:00 AM 26-Apr-2012 12:00:00 AM
6 rows selected.
SQL>

Similar Messages

  • Sql query to find the balances for a customer account wise.

    Hi,
    Could someone help me with the sql query to find the balances for each customer account wise. This is need to generate the report.
    presently we are using this query, but the output doesnot return the expected result.
    SELECT sum(nvl(ps.acctd_amount_due_remaining,0)) "Balance"
    FROM      ra_cust_trx_line_gl_dist_all gld,
              gl_code_combinations c,
              ar_payment_schedules_all ps,
              RA_CUSTOMER_TRX_ALL rat,
              ra_customers rc
    WHERE      c.CHART_OF_ACCOUNTS_ID = 101
    and gld.code_combination_id = c.code_combination_id
         and rat.CUSTOMER_TRX_ID=gld.CUSTOMER_TRX_ID
         and rat.CUSTOMER_TRX_ID=ps.CUSTOMER_TRX_ID
    and ps.customer_id=rc.customer_id
         and ps.status='OP'
         and ps.gl_date <= :PDATE
         and ps.org_id=:PORGID
         and ps.class in ('GUAR','INV','DM','DEP')
    and c.SEGMENT4=:Account_id
    and ps.customer_id=:Customer_id
    Thanks in advance.
    Kalyan.

    Can someone help us with this.

  • Sql query to indetify the identical data

    Hi
    I would like to know the query to identify the identical data in same table.
    Example: I have table called 'AA" having same data for load_ctry i.e. 'MY' and 'HK' ,I need sql query to indetify the identical data for above countries in that table.
    Thanks
    MR

    Hi,
    Table called "AA" with columns col1,col2,col3,load_ctry with following sample date
    col1 col2 col3 load_ctry
    3 bg xx TH
    4 op xc TH
    3 bg xx MY
    4 op xc MY
    3 vb nb MY
    When query ,if data is identical for both countries ,no data to fecth or data identical .If data is differ ,it will return the mismatching data becuase we are using same table for both countries .
    Thanks
    MR

  • Can anybody provide the SQL query to find the files uploaded in a particular folder?

    Hi All,
    Can anybody provide the SQL query to find the documents (document name) uploaded in a particular folder? While clicking on folder in
    GUI I'm hitting the Timeout error. I would like to find the files uploaded into this folder from SQLPLUS.
    Any help is greatly appreciated.
    With best regards,
    Nevin

    Nevin,
    Be great if we could know the version of Portal. For Rel. 1, here's the query
    select id,masterthingid from wwv_things
    where siteid = &site
    and cornerid = &corner
    &site - Content Area id
    &corner - Folder id
    if you don't know the folder id, use
    select id from wwv_corners where siteid = &site
    and name = &folder
    Hope this helps. I have run into this situation before. Usually, the culprits were
    one of the following:
    1. Junk Characters in description of item (caused due to Copy-Paste)
    2. Special Characters in the File name
    Hi All,
    Can anybody provide the SQL query to find the documents (document name) uploaded in a particular folder? While clicking on folder in
    GUI I'm hitting the Timeout error. I would like to find the files uploaded into this folder from SQLPLUS.
    Any help is greatly appreciated.
    With best regards,
    Nevin

  • Sql query to find the space in between the sentense

    hi,
    can anybody tell me the query to find the space in between the data
    chandan

    Not sure what you mean, but you might want to take a look at INSTR, like in
    instr (txt, ' ')

  • SQL Query to find the Notify upon Completion employees

    Hi,
    I am looking for a query to find the names of the employees/users who are notified upon the completion of the Concurrent Programs. I know how to find the concurrent program details but not able to find notify upon completion employees details. Please help me.
    Thanks,
    KM

    Thanks Scott for responding. There is no any custom code. Here is the navigation to reach there:
    System Administrator -> Requests -> View -> Specific Requests -> Open any request ->View Details -> Options -> Notify the following people
    I am looking for the query to find these notified people using SQL Query.Query FND_CONCURRENT_REQUESTS table (NOTIFY_ON_PP_ERROR column) -- http://etrm.oracle.com/pls/et1211d9/etrm_pnav.show_object?c_name=FND_CONCURRENT_REQUESTS&c_owner=APPLSYS&c_type=TABLE
    Thanks,
    Hussein

  • SQL Query to find the Delta between 2 rows

    Can the below be possible? If so can you help me writing aSQL Query to do so…
    I have data in spreadsheet which is pulled off from the database (data from more than one table with joins); send it to the different teams. They will check the data n update the spreadsheet if necessary and send it back to me.
    I have to find the changes and update the database from the provided spreadsheet accordingly. Changes can be on different columns on each set of row.
    Example:
    DataFrom
    ServerName
    Branch_Name
    Application_Name
    Server Status
    Application_Status
    App_Environment
    Tier
    SQL Query
    abcdef
    app
    adp
    Deployed
    Deployed
    Production
    silver
    Excel
    abcdef
    app
    adp
    Deployed
    Deployed
    Development
    Bronze
    DataFrom
    ServerName
    Branch_Name
    Application_Name
    Server Status
    Application_Status
    App_Environment
    Tier
    SQL Query
    Hijkl
    app
    adp
    Deployed
    Deployed
    Production
    Gold
    Excel
    Hijkl
    app
    Dep
    Deployed
    Deployed
    Production
    Gold
    DataFrom
    ServerName
    Branch_Name
    Application_Name
    Server Status
    Application_Status
    App_Environment
    Tier
    SQL Query
    Xzy
    app
    Dep
    Deployed
    Deployed
    Production
    Silver
    Excel
    Xzy
    App
    Dep
    Deployed
    Deployed
    Development
    Silver
    Above scenario is an example what I am look to do with sql script? Opinions/queries accepted…
    There are 1200+ rows to compare it manually which is a pain.
    Thanks.

    Columns are different, when the contain multiple distinct values.
    SELECT COUNT(DISTINCT Name) ,
    COUNT(DISTINCT GroupName) ,
    COUNT(*)
    FROM HumanResources.Department;
    Without a concise and complete example (table DDL and sample data insert statements), it's hard to tell what the correct solution could be..
    DECLARE @Sample TABLE ( SetID INT, ServerID INT, ApplicationID INT );
    INSERT INTO @Sample
    VALUES ( 1, 1, 1 ),
    ( 1, 1, 1 ),
    ( 2, 1, 1 ),
    ( 2, 1, 2 ),
    ( 3, 1, 1 ),
    ( 3, 2, 1 );
    WITH Evaluate AS
    SELECT SetID,
    COUNT(DISTINCT ServerID) AS Servers,
    COUNT(DISTINCT ApplicationID) AS Applications
    FROM @Sample
    GROUP BY SetID
    SELECT S.*,
    CASE WHEN E.Servers != 1 THEN 1 ELSE 0 END AS ServersDifferent,
    CASE WHEN E.Applications != 1 THEN 1 ELSE 0 END AS ApplicationsDifferent
    FROM @Sample S
    INNER JOIN Evaluate E ON S.SetID = E.SetID
    ORDER BY S.SetID;

  • SQL query to correct the corrupted data

    Hi All,
    I got strucked up with a query correcting the corrupted records.
    CREATE TABLE EX1
    EMPID INTEGER,
    DW_EFF_DT DATE,
    DW_EXPR_DT DATE
    INSERT INTO EX1 VALUES(1,'04-MAR-1998','13-MAR-1999');
    INSERT INTO EX1 VALUES(1,'14-MAR-1999','02-MAY-2000');
    INSERT INTO EX1 VALUES(1,'03-MAY-2000','01-MAY-2013');
    INSERT INTO EX1 VALUES(1,'02-MAY-2013','31-DEC-9999');
    I have empid with other attributes,we are maintaing history.There is some data which is corrupted and we need to correct it.
    DW_EFF_DT which is less than '01-FEB-2005' should be made by default '31-JAN-2005'
    Again the data should be corrected and my output should be like the below
    EMPNO     DW_EFF_DT     DW_EXPR_DT
    1     2005-01-27     2005-01-28
    1     2005-01-29     2005-01-30
    1     2005-01-31     2013-05-01
    1     2013-05-02     9999-12-31
    I used the lead and lag function,but it is applied sequentially.
    How I can get the dates subtratcing by 1 for each and every alternative row.
    I tried the below query and able to achive half of it.
    SELECT A.* ,COALESCE(LEAD(NEW_DW_EFF,1) OVER(ORDER BY NEW_DW_EFF) -1,TO_DATE('31-DEC-9999','DD-MON-YYYY')) AS NEW_DW_EXPR_DT
    FROM
    SELECT ID,
    DW_EFF_DT,
    DW_EXPR_DT,
    CASE WHEN DW_EFF_DT<TO_DATE('01-FEB-2005','DD-MON-YYYY') THEN TO_DATE('31-JAN-2005','DD-MON-YYYY') ELSE DW_EFF_DT END AS NEW_DW_EFF
    FROM EX1
    )A
    ID     DW_EFF_DT     DW_EXPR_DT     NEW_DW_EFF     NEW_DW_EXPR_DT
    1     04-MAR-98     13-MAR-99      31-JAN-05      30-JAN-05
    1     14-MAR-99     02-MAY-00      31-JAN-05      30-JAN-05
    1     03-MAY-00      01-MAY-13      31-JAN-05      01-MAY-13
    1     02-MAY-13      31-DEC-99      02-MAY-13      31-DEC-99
    Please help me in this regard.i am still trying it.
    Thanks in advance,
    KVB

    KVB wrote:
    EMPNO     DW_EFF_DT     DW_EXPR_DT
    1     2005-01-27     2005-01-28
    1     2005-01-29     2005-01-30
    1     2005-01-31     2013-05-01
    1     2013-05-02     9999-12-31
    Actually the last record is the active record.
    Th 3rd record effectiuve date is adjusted because the dateis less than 2005-02-01. So that we adjusted to 2005-01-31,
    The 2nd record expiry date will be adjusted to 3rd record effective date-1,
    Now the 2nd record effective will be adjusted to 2nd record expiry date-1 and so on..until the first record.
    Actually I could not explain much better in business point of view like why they do this.Technically we need to achive this through SQL.
    CheersOkay, that makes more sense, thanks :)
    Probably not the prettiest ... but should work (you may need to tweak it a bit)
    ME_TUBBZ? select
      2        empid
      3     ,  to_date('2005-02-01','yyyy-mm-dd')  - ( 2* rn) + 1 as new_eff_date
      4     ,  case
      5           when rn = 1
      6           then
      7              dw_expr_dt
      8           else
      9              to_date('2005-02-01','yyyy-mm-dd') - (2 * (rn - 1))
    10        end
    11              as new_exp_date
    12     ,  dw_eff_dt
    13     ,  dw_expr_dt
    14  from
    15  (
    16     select
    17           empid
    18        ,  dw_eff_dt
    19        ,  dw_expr_dt
    20        ,  row_number() over (partition by empid order by dw_eff_dt desc)             as rn
    21     from ex1
    22     where dw_eff_dt < to_date('2005-02-01','yyyy-mm-dd')
    23  )
    24  order by dw_eff_dt  asc
    25  /
                 EMPID NEW_EFF_DATE         NEW_EXP_DATE         DW_EFF_DT            DW_EXPR_DT
                     1 27-JAN-2005 00 00:00 28-JAN-2005 00 00:00 04-MAR-1998 00 00:00 13-MAR-1999 00 00:00
                     1 29-JAN-2005 00 00:00 30-JAN-2005 00 00:00 14-MAR-1999 00 00:00 02-MAY-2000 00 00:00
                     1 31-JAN-2005 00 00:00 01-MAY-2013 00 00:00 03-MAY-2000 00 00:00 01-MAY-2013 00 00:00
    3 rows selected.
    Elapsed: 00:00:00.01
    ME_TUBBZ? Cheers,

  • Issue with query to find the lag data

    Hello PL/SQL Gurus/Experts,
    I am not able to extract the data with the following table structure -
    drop table T2;
    create table T2(Stream, Trade, Fees) as select
    'MECHNICAL', 'Primary', '5534500' from dual union all select
    'ELECTRICAL', 'Secondary', '5285500' from DUAL union all select
    'MECHNICAL', 'Secondary', '2364535' from dual union all select
    'ELECTRICAL', 'Primary', '1734540' from DUAL union all select
    'MBE', 'Secondary', '3424500' from dual union all select
    'ELECTRONICS', 'Primary', '5004567' from DUAL union all select
    'ELECTRONICS', 'Secondary', '4543200' from DUAL union all select
    'COMPUTERS', 'Secondary', '5534500' from DUAL union all select
    'CIVIL', 'Primary', '2345500' from DUAL union all select
    'CIVIL', 'Secondary', '4456500' from DUAL union all select
    'COMPUTERS', 'Primary', '9542500' from DUAL;I want to extract the data in following format -
    Stream          Trade          Fees
    MECHNICAL     Primary          5534500
              Secondary          2364535
    ELECTRICAL     Primary          1734540
              Secondary       5285500
    ELECTRONICS     Primary          5004567
              Secondary        4543200
    CIVIL          Primary          2345500
              Secondary         4456500
    MBE          Primary     
              Secondary       3424500
    Total                    34693342Now if if i am using the following query -
    SELECT CASE WHEN LAG(Stream||Trade) OVER (ORDER BY Stream, Trade, Fees) = Stream||Trade THEN NULL ELSE Stream END Stream
         , CASE WHEN LAG(Stream||Trade) OVER (ORDER BY Stream, Trade, Fees) = Stream||Trade THEN NULL ELSE Trade END Trade
         , CASE WHEN LAG(Fees) OVER (partition by Stream||Trade ORDER BY Stream, Trade, Fees) = Fees THEN NULL ELSE Fees END Fees
    FROM (     select distinct Stream, Trade, Fees from T2 ORDER BY Stream, Trade, Fees);
    SELECT  NVL2(LAG(Trade) OVER (PARTITION BY Stream, Trade ORDER BY Stream, Trade, Fees),Null,Trade) Trade,
         NVL2(LAG(Stream) OVER (PARTITION BY Stream, Trade ORDER BY Stream, Trade, Fees),Null,Stream) Stream,
         Fees
    from (select distinct Stream, Trade, Fees from T2)Kindly help me ...
    Thank you all for your time and effort in advance.

    This way!!!
    select a.stream2 stream, trade, sum(fees)
      from (
            select case row_number() over(partition by stream order by trade)
            when 1 then stream
            else 'Unknown' end stream2,trade, fees
            from t2
            order by stream, trade
           ) a
    where a.stream2 != 'Unknown'
    group by a.stream2, trade;
    STREAM      TRADE     SUM(FEES)             
    ELECTRICAL  Primary   1734540               
    MECHNICAL   Primary   5534500               
    COMPUTERS   Primary   9542500               
    MBE         Primary                         
    ELECTRONICS Primary   5004567               
    CIVIL       Primary   2345500PS:- Use instead of {CODE}
    Also, use != instead of <> while pasting the code on Forum. <> is converted to NULL or probably ignored.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Sql query to find the last word in a sentence (column)

    Hi,
    I have to retrieve the last word in a column containing varchar2(text) values.
    If anyone knows the query for this kindly share with me.

    There's also the option of regular expressions in 10g onwards...
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (select 'oracle forums is the best resource for oracle intelligence' as l from dual)
      2  select regexp_replace(l, '[[:print:]]* ([^ ]*)$','\1') as l
      3* from t
    SQL> /
    L
    intelligence
    SQL>

  • Looking for a query to find first/last dates in overlapping dates...

    Hi,
    I'm looking for a query to find the first dates and last dates in a table conaining overlapping dates.
    I have a subscription table which has for each Customer start and end date for different subscriptions.
    I want to know the different ranges of date where there is subscriptions active.
    so if the table has this:
    CustID, Start date, end date
    1, 2008-01-01, 2012-06-06
    1 ,2009-01-01, 2011-01-01
    1, 2011-01-01, 2013-02-02
    1, 2013-01-01, 2013-08-08
    1, 2014-01-01, 2014-04-04
    I want to produce this result:
    custid, range start, range end
    1, 2008-01-01, 2013-08-08
    1, 2014-01-01, 2014-04-04
    the first row is the range identified from the 4 rows in my subscription table.
    thanks :)

    I think I found it...
    http://stackoverflow.com/questions/5213484/eliminate-and-reduce-overlapping-date-ranges
    let me try this method
    Hi,
    m writing to follow up with you on this post. Thanks for you posting a reply to share your workground. Was the problem resolved after performing the above link? If you are satisfied with the above solution, I’d like to mark this issue as "Answered".
    Please also feel free to unmark the issue, with any new findings or concerns you may have.
    Thanks,
    Sofiya Li
    If you have any feedback on our support, please click here.
    Sofiya Li
    TechNet Community Support

  • Query to find the memory of database in oracle,sql

    Hi All,
    Please let me know the query to find the memory of database in oracle,sql.
    Thanks,
    sajith

    How do I find the overall database size?
    The biggest portion of a database's size comes from the datafiles. To find out how many megabytes are allocated to ALL datafiles:
    select sum(bytes)/1024/1024 "Meg" from dba_data_files;
    To get the size of all TEMP files:
    select nvl(sum(bytes),0)/1024/1024 "Meg" from dba_temp_files;
    To get the size of the on-line redo-logs:
    select sum(bytes)/1024/1024 "Meg" from sys.v_$log;
    Putting it all together into a single query:
    select a.data_size+b.temp_size+c.redo_size "total_size"
    from ( select sum(bytes) data_size
    from dba_data_files ) a,
    ( select nvl(sum(bytes),0) temp_size
    from dba_temp_files ) b,
    ( select sum(bytes) redo_size
    from sys.v_$log ) c;
    Another query ("Free space" reports data files free space):
    col "Database Size" format a20
    col "Free space" format a20
    select round(sum(used.bytes) / 1024 / 1024 ) || ' MB' "Database Size"
    , round(free.p / 1024 / 1024) || ' MB' "Free space"
    from (select bytes from v$datafile
    union all
    select bytes from v$tempfile
    union all
    select bytes from v$log) used
    , (select sum(bytes) as p from dba_free_space) free
    group by free.p
    This is what I use :P From http://www.orafaq.com/wiki/Oracle_database_FAQ#How_do_I_find_the_overall_database_size.3F

  • Query to find the  second maximum date in a table

    please give me the query to find the second maximum date in a table

    You can try with this
    SELECT empno
          ,hiredate
      FROM emp        a
    WHERE 2          = (SELECT COUNT(DISTINCT hiredate)
                           FROM emp        b
                          WHERE b.hiredate      >= a.hiredate
    OR
    SELECT empno
          ,hiredate
      FROM (SELECT ROWNUM      row_num
                  ,empno
                  ,hiredate
              FROM emp        a
          ORDER BY hiredate   ASC
    WHERE row_num             = 2;Regards
    Arun

  • Query to find the user is having access to sql server DB

    Hi,
    Please help me in this.
    Query to find the whether the user is having access to sql server DB.
    Cheers,
    sajith

    TUBBY_ORCL?Select 1 from dual where 'ORACLE' = 'SQL SERVER';
    no rows selected
    Elapsed: 00:00:00.01

  • Write the sql query to find largest value in row wise without using   great

    write the sql query to find largest value in row wise without using
    greatest fuction?

    Another not so good way, considering you want greatest of 4 fields from a single record:
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (Select 100 col1,200 col2,300 col3,400 col4 from dual
      2  union select 500,600,700,800 from dual
      3  union select 900,1000,1100,1200 from dual
      4  union select 1300,1400,1500,1600 from dual
      5  union select 1700,1800,1900,2000 from dual
      6  union select 2100,2200,2300,2400 from dual
      7  union select 2800,2700,2600,2500 from dual
      8  union select 2900,3000,3100,3200 from dual)
      9  SELECT (CASE WHEN col1 > col2 THEN col1 ELSE col2 END) Max_value
    10  FROM
    11  (SELECT (CASE WHEN col1_col2 > col2_col3 THEN col1_col2 ELSE col2_col3 END) col1,
    12         (CASE WHEN col2_col3 > col3_col4 THEN col2_col3 ELSE col3_col4 END) col2,
    13         (CASE WHEN col3_col4 > col4_col1 THEN col3_col4 ELSE col4_col1 END) col3
    14  FROM
    15  (SELECT (CASE WHEN col1 > col2 THEN col1 ELSE col2 END) col1_col2,
    16         (CASE WHEN col2 > col3 THEN col2 ELSE col3 END) col2_col3,
    17         (CASE WHEN col3 > col4 THEN col3 ELSE col4 END) col3_col4,
    18         (CASE WHEN col4 > col1 THEN col4 ELSE col1 END) col4_col1
    19* FROM t))
    SQL> /
    MAX_VALUE
           400
           800
          1200
          1600
          2000
          2400
          2800
          3200
    8 rows selected.
    SQL> Edited by: AP on Sep 21, 2010 6:29 AM

Maybe you are looking for