DBAsupport.com Forums - Powered by vBulletin
Results 1 to 4 of 4

Thread: Fetch dates between from months and years

Hybrid View

  1. #1
    Join Date
    Mar 2010
    Posts
    6

    Fetch dates between from months and years

    i'm unable to fetch dates, details are as follows:-

    Input parameter is - dd/mm/yyyy

    Table data - Column Month and Year.

    Current query is

    select * from table_name tn where
    (tn.month > '10' and tn.year = '2009') or
    (tn.year = '2009' and tn.month <'12'))

    // problem is that this will fetch duplicate records as months <'12' will also return duplicate data.

    How to implement between here in columns where months and year are seperate.

    Please advise.

  2. #2
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,555
    Don't even want to know why a date is stored as separate varchar2() columns but this may help you in describe scenario...
    Code:
    SQL> 
    SQL> create table table_name(
      2  month varchar2(2),
      3  year  varchar2(4)
      4  );
    
    Table created.
    
    SQL> insert into table_name values ('09','2009');
    
    1 row created.
    
    SQL> insert into table_name values ('10','2009');
    
    1 row created.
    
    SQL> insert into table_name values ('11','2009');
    
    1 row created.
    
    SQL> insert into table_name values ('12','2009');
    
    1 row created.
    
    SQL> insert into table_name values ('01','2010');
    
    1 row created.
    
    SQL> commit;
    
    Commit complete.
    
    SQL> select  *
      2  from    table_name tn
      3  where   to_date(tn.month||tn.year,'mmyyyy') 
      4          between to_date('102009','mmyyyy') 
      5              and to_date('122009','mmyyyy')
      6  ;
    
    MO YEAR
    -- ----
    10 2009
    11 2009
    12 2009
    
    SQL>
    Please take into consideration "between" returns both ends of the range.
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

  3. #3
    Join Date
    Mar 2010
    Posts
    6
    Thanks Paul,

    But still problem presists, as the table column for month and year are of type numbers..

    ORA-01843: not a valid month is coming.

    How to cast this here.

  4. #4
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,555
    Quote Originally Posted by ketansetia View Post
    But still problem presists
    I see... you didn't tell us everything on first post, don't you ))

    Quote Originally Posted by ketansetia View Post
    as the table column for month and year are of type numbers..
    Use to_char() to convert them from number to chars... be sure you format them correctly, two chars for month, four chars for year.

    Quote Originally Posted by ketansetia View Post
    ORA-01843: not a valid month is coming.
    If wrongly formatted data, format it correctly.
    If invalid data, fix it.
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width