Between clause with DECODE function.

Hi All,
I have one table which has object, date and quantity columns, Now based on date range I can find out the objects and quantity but the question is: I want a query which returns all the objects and should show quantity 0(zero or null) for those objects which don't falls under that date range. So I thought to use a decode function but it only uses the relational operation, I thought something like this but it's wrong,
select object,decode(created_date,BETWEEN '01/01/2008' TO '08/01/2008',quantity,0) from table;
Does anyone have any idea how can I make this work in single sql statement?
Thanks for your help.

select object,decode(created_date,BETWEEN '01/01/2008' TO '08/01/2008',quantity,0) from table;
Does anyone have any idea how can I make this work in single sql statement?If you're still interested in using DECODE over Case when, please try this.
Check for employees hired between 6/17/1987 and 9/30/1987
SQL> select first_name||' '||last_name as emp_name, hire_date,
  2         decode(sign((to_date('06/17/1987','MM/DD/YYYY')-1)-hire_date)
  3           + sign((to_date('09/30/1987','MM/DD/YYYY')+1)-hire_date)
  4                   ,0,'Between','Not between') as IsBetween
  5    from employees
  6   where hire_date <to_date('01/01/1991','MM/DD/YYYY')
  7  /
EMP_NAME                                       HIRE_DATE ISBETWEEN
Steven King                                    17-JUN-87 Between
Neena Kochhar                                  21-SEP-89 Not between
Alexander Hunold                               03-JAN-90 Not between
Jennifer Whalen                                17-SEP-87 BetweenMessage was edited by:
Bobbydj

Similar Messages

  • Prob with decode function

    hi iam having prob with the following decode function
    declare
    c varchar2(20);
    begin
    select decode(deptno,
    10, 'accounting',
    20,'Research',
    30 ,'sales',
    40,'operations','UNKNOWN') into c from dept where loc='DALLAS';
    dbms_output.put_line('DEPARTMENT in DALLAS'||c);
    select decode(deptno,
    10, 'accounting',
    20,'Research',
    30 ,'sales',
    40,'operations','UNKNOWN') into c from dept where loc='INDIA';
    dbms_output.put_line('DEPARTMENT IN INDIA'||c);
    end;
    iam getting no_data_found exception which is reasonable..but what happened to 'unknown' clause in decode function.
    thank u
    rajiv

    Please see the responses to your prob in decode function
    John

  • Error while replacing IF statements with DECODE function in procedure

    Hi All,
    I have created a procedure which has nested IF statements. Now I want to replace the IF statements with DECODE functions to improve performance.
    Procedure:
    IF (var_int_sev = '0')
    THEN
    var_sev := '2';
    ELSE
    SELECT sev
    INTO var_int_sev
    FROM errorconfig
    WHERE errorcode = var_errorcode;
    var_sev := var_int_sev;
    END IF;
    I converted the above IF statement into DECODE function as mentioned below:
    var_Sev := DECODE(var_int_sev,0,2,SELECT severity FROM errorconfig WHERE errorcode=var_ErrorCode)
    But it throws below error at the select statement used inside DECODE.
    Error(58,51): PLS-00103: Encountered the symbol "SELECT" when expecting one of the following: ( - + case mod new not null others <an identifier> <a double-quoted delimited-identifier> <a bind variable> avg count current exists max min prior sql stddev sum variance execute forall merge time timestamp interval date <a string literal with character set specification> <a number> <a single-quoted SQL string> pipe <an alternatively-quoted string literal with character set specification> <an alternativ
    Can someone help me in converting the IF to DECODE in the above case. Also how can we use a select statement inside decode.

    instead of trying to rewrite all your code and hoping that the performance will be better, it's a better option to investigate and find out which part of your application is slow
    read this:
    When your query takes too long ...

  • OWB3i - Validate with DECODE function

    Hi,
    in OWB 3i when I validate an expression defined inside an expression operator with DECODE() function (which is not include in default transformation) come this error:
    Line 1, Col 1:
    PLS-00204: function or pseudo-column 'DECODE' may be used inside a SQL statement only
    but it seems only a warning because when i generate the scripts and run no other error happen and all works fine.
    Regards

    Hi,
    Welcome to the forum!
    When you use a default value, the last argument to DECODE is the actual value you want as a default.
    For example:
    SELECT       ename
    ,       deptno
    ,       DECODE ( deptno
                 , 30     , 'Sales'
                      , 'All others'     -- Default value
                  )                 AS dname
    FROM      scott.emp
    ORDER BY  ename
    ;Output:
    ENAME          DEPTNO DNAME
    ADAMS              20 All others
    ALLEN              30 Sales
    BLAKE              30 Sales
    CLARK              10 All others
    FORD               20 All others
    JAMES              30 Sales
    JONES              20 All others
    KING               10 All others
    MARTIN             30 Sales
    MILLER             10 All others
    SCOTT              20 All others
    SMITH              20 All others
    TURNER             30 Sales
    WARD               30 Sales 
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
    Explain, using specific examples, how you get those results from that data.
    If you can show the problem using commonly available tables (such as those in the scott schema) then you don't need to post any sample data; just the results and the explanation.
    Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
    See the forum FAQ {message:id=9360002}

  • Having clause with Analytic function

    can you pls let me know if we can use HAVING clause with analytic function
    select eid,empno,sum(sal) over(partition by year)
    from employee
    where dept = 'SALES'
    having sum(sal) > 10000I m getting error while using the above,
    IS that we can use HAVING clause with partition by
    Thanks in advance

    Your having clause isn't using an analytical function, is using a regular aggregate function.
    You also can't use analytical functions in the where clause or having clause like that as they are windowing functions and belong at the top of the query.
    You would have to wrap the query to achieve what you want e.g.
    SQL> ed
    Wrote file afiedt.buf
      1  select deptno, total_sal
      2  from (
      3        select deptno,sum(sal) over (partition by deptno) as total_sal
      4        from   emp
      5       )
      6  group by deptno, total_sal
      7* having total_sal > 10000
    SQL> /
        DEPTNO  TOTAL_SAL
            20      10875
    SQL>

  • ViewObjects Order by clause with DECODE

    Hello!
    I am using Jdeveloper 11g, version 11.1.1.2.0.
    The problem I'm having is this.
    If I use a DECODE statement in view objects ORDER BY clause, I get an error: "java.util.regex.PatternSyntaxException: Unmatched closing ')' near index 2".
    Let me give an example. I'll be using EmployeesView view object, which is using Employees entity from HR schema.
    This is a part of view objects XML.
    <ViewObject
    xmlns="http://xmlns.oracle.com/bc4j"
    Name="EmployeesView"
    Version="11.1.1.55.36"
    SelectList="Employees.EMPLOYEE_ID,
    Employees.FIRST_NAME,
    Employees.LAST_NAME,
    Employees.EMAIL,
    Employees.PHONE_NUMBER,
    Employees.HIRE_DATE,
    Employees.JOB_ID,
    Employees.SALARY,
    Employees.COMMISSION_PCT,
    Employees.MANAGER_ID,
    Employees.DEPARTMENT_ID"
    FromList="EMPLOYEES Employees"
    BindingStyle="OracleName"
    CustomQuery="false"
    PageIterMode="Full"
    UseGlueCode="false"
    OrderBy="Employees.MANAGER_ID">
    As you can see in this case, the Order by clause is very simple. This works like a charm.
    But, if put something like this "DECODE(Employees.MANAGER_ID, NULL, 1, 2)" in the Order by, I get an internal parsing error.
    I replicated this error on my home machine as well as on my work machine. I'm using the same version of Jdeveloper on both.
    Has anyone else stumbled upon this problem and solved it?
    Any thoughts would be greatly appreciated :)
    Kristjan

    The second example works, but the first one doesn't, unfortunately :/
    Also, the example I gave is unfortunately just that, a proof-of-concept example that there is a problem with DECODE if it is written inside the Order by clause.
    My real DECODE use case is a bit different. Like this: "DECODE(attribute, 'N', 1, 2) ASC".
    Since posting my original question, I did some research-by-example work and I discovered that this is not just a problem of DECODE, but more like a problem of brackets and commas.
    No database function that uses more than one parameter can be used in Order by clause.
    The reason is, if a function with more than one parameter is used, commas inside brackets have to be used. Something along the lines of: "database_function(param1, param2, ...)".
    The parser seems to have a problem with this kind of expressions.
    Is there a work around?
    Kristjan
    p.s.: Thank you for your quick response.

  • Order by clause using decode function

    Hi everybody,
    i need below order in my report.
    Connecticut
    greenwich
    stamford
    bridgeport
    New York
    NYC
    wrestcher
    byram
    Georgia
    atlanta
    athens
    oconny
    first i need above order in my view out put.
    so in order by clause i used first decode function for State ordering
    and in second decode function for city ordering.
    i do not need order by ascending or descending.
    so pls anybody can help me.
    any help is greatly appreciated.
    thanks.

    add asc after the decode. default is desc

  • Problem with decode function.

    Hi,
    Can anyone of you help me out in solving this?
    It is like i wish to give different select statements according to the value of a parameter entered by user USING DECODE FUNCTION.The select statement contains some other select statements inside it.So when i execute it,it is giving error like 'ORA-00913-too many values(even when i enclose select statements within brackets).

    ORA-00913 too many values
    Cause: The SQL statement requires two sets of values equal in number. This error occurs when the second set contains more items than the first set. For example, the subquery in a WHERE or HAVING clause may return too many columns, or a VALUES or SELECT clause may return more columns than are listed in the INSERT.
    Action: Check the number of items in each set and change the SQL statement to make them equal.
    the above is from oracle documentation. the brackets is not the problem, u must be using multiple items in the integrated decode queries. If the problem still exists post ur DML for further analysis.
    zaibi.

  • USING BETWEEN CLAUSE WITH 'IN'.

    Can I use between function with IN.
    Example.
    select a.* from emp a, dept b
    where a.deptid=b.deptid
    and a.deptid in (
    a.deptid between '122' and '199', '330'
    Actually its giving an errorr
    ORA-00907: missing right parenthesis*

    This is not a correct syntax. Use multiple between condition with "OR".
    Regards,
    Vidya.

  • How to convert rows into columns with decode function

    Hi,
    How to convert rows into columns with the help of decode function in oracle.
    thanks and regards
    P Prakash

    say
    col1 col2
    1 10
    2 20
    3 30
    then use
    select col1,
    sum(decode(col2,10,10)) "new1"
    sum(decode(col2,20,20))"new2"
    sum(decode(col2,30,30))"new3"
    from table_name
    group by col1;
    we used sum u can use ny function if wont u have to give the column name i.e col2 name also
    so i think u got it nw
    regards

  • Need help with DECODE function

    Hello,
    I am trying to use default within the decode function and every time I get a missing expression. I have searched everywhere and cant figure out what I'm doing wrong. Thanks
    select decode (request_id,0,'No files found', DEFAULT)

    Hi,
    Welcome to the forum!
    When you use a default value, the last argument to DECODE is the actual value you want as a default.
    For example:
    SELECT       ename
    ,       deptno
    ,       DECODE ( deptno
                 , 30     , 'Sales'
                      , 'All others'     -- Default value
                  )                 AS dname
    FROM      scott.emp
    ORDER BY  ename
    ;Output:
    ENAME          DEPTNO DNAME
    ADAMS              20 All others
    ALLEN              30 Sales
    BLAKE              30 Sales
    CLARK              10 All others
    FORD               20 All others
    JAMES              30 Sales
    JONES              20 All others
    KING               10 All others
    MARTIN             30 Sales
    MILLER             10 All others
    SCOTT              20 All others
    SMITH              20 All others
    TURNER             30 Sales
    WARD               30 Sales 
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
    Explain, using specific examples, how you get those results from that data.
    If you can show the problem using commonly available tables (such as those in the scott schema) then you don't need to post any sample data; just the results and the explanation.
    Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
    See the forum FAQ {message:id=9360002}

  • Odd Error with decode function in Order By Clause

    I am trying to compile a procedure and can't get around an error with a dynamic order by that doesn't make much sense to me. I can repoduce the error with a plain select statment in sql plus so I can rule out a declaration error. Here is an example with 2 numeric columns and a date column.
    select task_id, display_date, remark_id from task_list
    where task_id > 1000
    order by decode('Task_ID', 'Task_ID',Task_ID, 'Display_Date', Display_Date, 'Remark_ID',Remark_ID)
    returns the error:
    select task_id, display_date, remark_id from task_list
    ERROR at line 1:
    ORA-00932: inconsistent datatypes: expected NUMBER got DATE
    I'm not sure why this error is occuring, because it doesn't even hit the Display_Date field in the Decode statment. If I take the Display_date out of the statement, it runs properly. If I change Display_Date to To_Char(Display_Date) it also runs fine, but the sorting is incorrect. Also I'm running 9.2, and do not want to use dynamic sql to build my query. Any Ideas out there as to the cause of this error?

    I did find a workaround to this issue by breaking the decode statment into three separate statement, but I still think that the way it is written above should work, and may be a bug, or an error that I don't understand fully.
    The Order by was rewritten like this:
    order by decode(pSort, 'Task_ID',Task_ID), decode(pSort, 'Display_Date', Display_Date),
    decode(pSort, 'Remark_ID',Remark_ID);
    Thanks

  • Help with Decode function

    Hello!!
    Can anyone please help me with the decode statement
    Table 1 Table2 Table3
    Id Id Code
    Volume Code
    For each of the codes in Table3 I need to find the corresponding Volume. Can I use decode in the select statement to this,
    Eg Code Volume
    ABC 20
    XYZ 10 etc etc.
    Thankyou all in advance.

    Your table structure is a little unclear from your post. I am assuming that what you posted was:
    Table 1
    Id
    Table2
    Id
    Code
    Table3
    Volume
    Code You can use decode if there are only a few values for table3. For example,
    SELECT table2.code,
    DECODE(table2.code,'ABC',10,'XYZ',20,'UNKNOWN') volume
    FROM table2
    or even
    SELECT code,DECODE(code,null,'NONE',
                      (SELECT volume FROM table3 where code=table2.code)) volume
    FROM table2However, the first will break, or at least be incorrect, if anyone adds a new code/volume pair, and is tedious if you have more than a few values. The second will likely be pretty slow if there are a large number of records in table3. The best solution would be to use a simple join rather than decode. Something like:
    SELECT table1.id, table2.code, table3.volume
    FROM table1, table2, table3
    WHERE table1.id = table2.id and
          table2.code = table3.code

  • Doubt with Decode function(By Naren)

    Hi all
    i have the following query
    SELECT S.SCHOOLNAME,
    SUM(DISTINCT DECODE(LR.QUESTIONNAIRE_ID,62, L.WORK_SPACE))/5 As LITSUM
    FROM TQDB_LEARNER_RESPONSE L LEFT JOIN TQDB_LEARNER_RESPONSERS LR ON LR.RESPONSER_ID=L.RESPONSER_ID LEFT JOIN
    SCHOOLS S ON S.SCHOOLID=LR.SCHOOL_ID
    GROUP BY S.SCHOOLNAME
    It is working fine if i fixed QUESTIONNAIRE_ID AS 62.
    Can i use parameter in place of 62 like :QID
    When i execute this query and gave 62 as parameter value it should give LITSUM depend on 62,And if i wont give value(null or blank) it should give result irrespective of QUESTONNAIRE_ID.
    I tried it by changing above query like following.
    SELECT S.SCHOOLNAME,
    SUM(DISTINCT DECODE(LR.QUESTIONNAIRE_ID,:QID, L.WORK_SPACE))/5 As LITSUM
    FROM TQDB_LEARNER_RESPONSE L LEFT JOIN TQDB_LEARNER_RESPONSERS LR ON LR.RESPONSER_ID=L.RESPONSER_ID LEFT JOIN
    SCHOOLS S ON S.SCHOOLID=LR.SCHOOL_ID
    GROUP BY S.SCHOOLNAME
    But it is not working properly,not giving expected result.
    Pls help me.

    never noticed that bit, sorry.
    I am assuming that your questionnaire_id is not null, so when you pass null there are no records with a questionnaire_id of null.
    if you want to total all L.WORK_SPACE if you pass null then try this
    SELECT S.SCHOOLNAME,
    SUM(DISTINCT DECODE(:QID, NULL, L.WORK_SPACE, DECODE(LR.QUESTIONNAIRE_ID, :QID, L.WORK_SPACE))) / 5 AS
    LITSUM
    FROM TQDB_LEARNER_RESPONSE L LEFT JOIN TQDB_LEARNER_RESPONSERS LR ON LR.RESPONSER_ID = L.RESPONSER_ID LEFT JOIN SCHOOLS S ON S.SCHOOLID = LR.SCHOOL_ID
    GROUP BY S.SCHOOLNAME;

  • Query with DECODE function

    My table has columns A,B & C with data as below
    A B C
    X 1 1
    1 X 1
    1 1 X
    Result should be all ‘Z’ inserted right to ‘X’ & must look like as below
    A B C
    X Z Z
    1 X Z
    1 1 X
    Please check the below query & help me to modify the query as the result for column b is not correct.
    select a,decode(b,'1','Z','X') b,decode(c,'1','Z','X') c
    from test;

    try
    With Data As
    Select 'X' A,'1' B, '1' C From Dual Union All
    Select '1','X','1' From Dual Union All
    Select '1','1','X' From Dual
    Select A, (Case When A ='X' And B='1' Then 'Z' Else B End) B,
    (Case When (A='X' Or B='X') And C='1' Then 'Z' Else C end)C 
    from dataVivek L

Maybe you are looking for

  • Can't find my creative cloud subscription to reinstall

    I purchased creative cloud and downloaded it successfully to my desktop. However, my desktop recently caught a virus and technician had to wipe out the ssytem and reinstall everything. However, I don't see the link to reinstall creative cloud. Can yo

  • Windows Vista with Windows 7 Upgrade on Boot Camp 4.0.1. Is it possible??

    Hello everyone, I own a genuine full installation Windows Vista and a genuine upgrade to Windows 7. I'd like to install these ( i.e. install Vista and then upgrade to 7) on a MacBookPro running on OSX Lion 10.7.2 with Boot Camp 4.0.1. Is It possible?

  • N95 8gb V20.0.0.16 Multimedia Key not working

    Hi, I think I found a bug! make sure the phone is in portrait mode: 1. press the multimedia key 2. press it again to exit 3. Turn the phone to bring it to landscape mode (make sure auto rotate is enabled) 4. press the multimedia key For me the menu d

  • JCO - Connection reset by peer - after 5 minute timeout

    Hello We're using SUP with the JCO Connector. This has been working flawlessly since project inception. Now, after switching to a new data center (Windows Server 2008 Server Enterprise Edition, SP2 on VMWare) we're observing a mysterious behavior wit

  • Missing font in incopy windows

    hello everyone, i've got another problem. I have 10 installations of Incopy PC (Win XP Prof) and installed severall (41) OpenType-fonts on these PCs.On 2 PCs everything works fine - Incopy recognises the fonts, but on 8 PCs 2 of the fonts where not s