NVL function

How to apply the nvl function to the case to avoid null values in the result (I want to replace nulls by zero in the result)

SUM (CASE
WHEN S.RESULT IN ('Y', 'O', 'I', 'F')
THEN
1
END
) STAT.

case statements can have ELSE clauses...

just put a 0 otherwise in there.

Tags: Database

Similar Questions

  • How to use the NVL function in decoding?

    Hi all

    How to use the NVL function in decoding?

    SELECT Decode (Sign (sum (nvl (7), 0)-nvl (sum (5), 0)), - 1, 0, (sum (nvl (7), 0)-nvl (sum (5), 0)) QTY)

    of the double

    Thank you

    You should not do that...

    Greatest (NVL (Sum (Quantity), 0)-NVL (SUM (quantity_received), 0), 0) AS qty_arrival

    will do the same

    HTH

  • NVL function in Oracle 11 G

    Hi all

    During my interview, I had two questions that made me perplexed in the interview.

    (1) in Oracle 11 G, there is a similar to the nvl function. What is it? I knew that we can use decode function nvl but in 11G, any function has been introduced?

    (2) other that function, what are the other ways to pass the entries of a procedure?

    Someone knew the answers to these or questions asked by the interviewer are wrong? Please give your suggestions...

    Hello

    user2639048 wrote:

    Hi all

    During my interview, I had two questions that made me perplexed in the interview.

    (1) in Oracle 11 G, there is a similar to the nvl function. What is it? I knew that we can use decode function nvl but in 11G, any function has been introduced?

    COALESCE may think of you (or the one who asked you this), but she is older than version 11.

    See COALESCE

    If you had no idea what was the service, a good answer would be: "I don't know what is the function, but I know that there is a"what's new"manual Oralce 11 (and all recent versions) and, better yet, a"What's new"chapter at the beginning of the manual of the SQL language that describes all the new features."  "Know exactly what are the new features in Oracle 11 aren't really important if we are now both Oracle 11 and older versions, anyway."

    (2) other that function, what are the other ways to pass the entries of a procedure?

    Someone knew the answers to these or questions asked by the interviewer are wrong? Please give your suggestions...

    Sorry, I can't understand what you're asking.

    Isn't a function.

    IN arguments are usually the best way to move the entries of a procedure.  If, in an interview, someone asked on other ways, make it clear that you know before you give any other answer.

    I suggest that you describe what you want a little more in detail.

    In addition, create a separate thread for each separate issue.  (I do not know what 2) above is, but it doesn't seem to have anything to do with 1).  (Asked in the same interview is not significant.)

  • How to use the NVL function to a parameter with comma delimited values

    Gurus,

    It is confusing to me.  I am trying to use the NVL function, but the setting that I'm passing in my cursor contains multiple values with commas.  The NVL function is confused when analyzing the values.

    'Where' cursor clause...

    and nvl (sn.c_attribute1,'x@#$%') in nvl (p_desig,'x@#$%'( )

    translated parameter values...

    and nvl (sn.c_attribute1,'x@#$%') in nvl ('SPRT''GOOD' 'BAD' 'x@#$%') -NVL does not parse the value correctly

    Any idea on how to get NVL recognize 'x@#$%' , if p_desig is null?

    Thank you

    Scott

    HI, Scott.

    Whenever you have a problem, please post a small example data (CREATE TABLE and only relevant columns, INSERT statements) of all the tables involved, so that people who want to help you can recreate the problem and test their ideas.

    Also post the results you want from this data, as well as an explanation of how you get these results from these data, with specific examples.

    Always say what version of Oracle you are using (for example, 11.2.0.2.0).

    See the FAQ forum: https://forums.oracle.com/message/9362002#9362002

    My best guess, based on what you posted bone is present, that your problem is discussed in the following:

    http://tkyte.blogspot.com/2006/06/varying-in-lists.html

    http://www.Oracle-base.com/articles/Misc/DynamicInLists.php

  • NVL function varies from char to varchar2 data type

    SQL> select * from v$version;
    
    BANNER
    --------------------------------------------------------------------------------
    Oracle Database 11g Release 11.2.0.1.0 - Production
    PL/SQL Release 11.2.0.1.0 - Production
    CORE    11.2.0.1.0  Production
    TNS for 32-bit Windows: Version 11.2.0.1.0 - Production
    NLSRTL Version 11.2.0.1.0 - Production
    
    SQL> create table fullJoinTest1(col1 char(14));
    
    Table created.
    
    SQL> create table fullJoinTest2(col1 char(14));
    
    Table created.
    
    SQL> insert all
      2  into fullJoinTest1 values('aaa')
      3  into fullJoinTest1 values('bbb')
      4  into fullJoinTest2 values('aaa')
      5  into fullJoinTest2 values('ccc')
      6  select 1 from dual;
    
    4 rows created.
    
    SQL> select nvl(a.col1,b.col1) as aaaa
      2    from fullJoinTest1 a full join fullJoinTest2 b
      3      on a.col1=b.col1
      4   where nvl(a.col1,b.col1)= 'ccc';
    
    no rows selected
    
    SQL> create table fullJoinTest3 as
      2  select nvl(a.col1,b.col1) as "WhatIsType?"
      3    from fullJoinTest1 a full join fullJoinTest2 b
      4      on a.col1=b.col1
      5   where nvl(a.col1,b.col1)= 'ccc';
    
    Table created.
    
    SQL> desc fullJoinTest3
     Name          Null?    Type
     ------------- -------- ------------
     WhatIsType?            VARCHAR2(14)
    
    SQL> create table fullJoinTest4 as
      2  select case when a.col1 is not null then a.col1 else b.col1 end as "WhatIsType?"
      3    from fullJoinTest1 a full join fullJoinTest2 b
      4      on a.col1=b.col1;
    
    Table created.
    
    SQL> desc fullJoinTest4
     Name        Null?    Type
     ----------- -------- --------
     WhatIsType?          CHAR(14)
    My question is why nvl function varies from char to varchar2 data type?
    Same case expression does not change the data type.
    I insist that this is a bug of function nvl isn't - it.

    Documented behavior:
    http://download.Oracle.com/docs/CD/E11882_01/server.112/e17118/functions119.htm#SQLRF00684

    If expr1 is character data, then Oracle Database converts expr2 to the data type of expr1
    before comparing them and returns VARCHAR2 in the character set of expr1.
    
  • Do I need to use the NVL function for this request?

    I seem to have some problems using the NVL function on a date. I'm trying to create a query pulls back just a date a file was sent between the sysdate and the date_sent more a minute. Here is the code I use...

    Oracle: 10.2 G
    SELECT date_sent as date_sent
    INTO    iDateSent
    FROM    tableX
    WHERE  file_id= 9999
    AND    date_sent BETWEEN sysdate AND date_sent + 1/1440; 
    Currently Output (null):
    DATE_SENT
    Desired output when date_sent is null:
    DATE_SENT
    0:0:000 0:00:00
    Note:
    date_sent column is in a normal date - same format as sysdate format.

    The problem I encounter is when date_sent is null, the stored procedure, I built crashes because date_sent returns a null and a null value cannot be stored in the variable 'iDateSent '. I was thinking about using the NVL function here to resolve, however, I'm not entirely sure who will work with the above query. Any thoughts?

    Published by: user652714 on July 9, 2010 12:58

    Published by: user652714 on July 9, 2010 13:11

    Hello

    you said: "desired output when date_sent is null: date_sent: 0".
    It is not possible to have a date of 'zero '.

    "and a null value cannot be stored in the variable 'iDateSent'. '.

    I do not think: If this variable is declared normally (without any not null not constrained to this topic) so that it can contain a null value. (it's 99% of the time in PL/SQL).

    I think it's that your SQL retrieves no rows, to manage what you should put an exception handler:

    begin
    SELECT date_sent as date_sent
    INTO    iDateSent
    FROM    tableX
    WHERE  file_id= 9999
    AND    date_sent BETWEEN sysdate AND date_sent + 1/1440;
    exception when NO_DATA_FOUND then
     iDateSent:=null;
    end;
    

    So what you can do if you don't want the date empty a nvl:

    return(nvl(iDateSent,sysdate));  -- or another date ...
    

    Published by: user11268895 on July 9, 2010 22:09

  • Decode the statement using the nvl function

    I inherited the code uses a decode and nvl. I'm not sure the purpose of him and wants confirmation that it is logical and an error. The situation can be re-created by implementing the following.

    CREATE TABLE XX_TEST
    (IDENTIFICATION NUMBER,
    DOMAIN VARCHAR2 (255)
    )

    INSERT INTO XX_TEST (ID, FIELD) VALUES (1, 'Yes')
    INSERT INTO XX_TEST (ID, FIELD) VALUES (2, 'No')
    INSERT INTO XX_TEST (ID, FIELD) VALUES (3, NULL)

    Commit

    SELECT ID,
    FIELD,
    DECODE (on the GROUND, the 'yes', ' Yes result this ", NVL (FIELD, 'No'),"no results don't do this","catch all result do this")
    OF XX_TEST

    I'm not sure of the NVL (FIELD, 'No') I think that the creator may have thought this would draw the NULL values, but it is not.
    Null is not converted into no. and plunges into the place the Tote. Can anyone help with an explanation of how oracle is interpretting the select statement. Any ideas on why NVL would be used like that? Seems a mistake?

    decode compares them the first FIELD against NVL (FIELD, 'No').
    If the FIELD is null estimated them VSN none which is not null, then...
    just to make an entry for the null value and one for the 'no' with the same result.
    However, I prefer case statement in this case.

    SELECT ID,
    FIELD,
    case
    when FIELD ='Yes' then 'Yes Result do this'
    when FIELD is null or FIELD = 'No' THen 'No result do this'
    else 'catch all result do this'
    end
    FROM XX_TEST
    

    As more readable.
    concerning

  • Not able to run the NVL function in the data model

    I tried to use NVL today in the data model, but I got the error message.
    Here's the code: -.
    SELECT ROUND (WHEN BOX: MO_TARGET_DISP_RATE > = 100)
    THEN 100
    (TO ANOTHER NVL (: MO_TARGET_DISP_RATE, 1) END, 2) AS "TARGET_PC_MO".

    Y at - it another option to replace NVL here... that runs in the NOSE? Thanks in advance.

    Just a suggestion, try a view then refer to the display in the data model

    (SELECT (ROUND (CASE WHEN to_number(:MO_TARGET_DISP_RATE) > = 100)))
    THEN 100
    Of OTHER NVL (to_number(:MO_TARGET_DISP_RATE), 1) END 2)), double) My_field

    The error was not a tank for the error number was or media...! If so above should be ok...

  • function nvl value null problem

    Hello

    I have a table named "SAL" with two columns "NAME" and "SALARY".
    Column 'NAME' got the varchar2 data type and column "SALARY" has got the number data type.

    Here is the command

    Select nvl(salary,0) from sal where name = "steve";

    Now if the table got an entry where name is steve, and the salary is null then it will return 0 because of the nvl function.

    But if he has no entry with the name steve the result is null.

    I want to if there is even no entry named steve in the table, the result should be 0 instead of null. What will be the command?

    Gul says:
    Hello thanks for your response. It solved my problem. Another question is what do I do if want to put the results of this command

    Select count (*) case when 0 then 0 max (salary) to another end wage
    by sal where name = "Steve";

    You can use NVL

    select nvl(max(salary),0) salary
    from sal where name = 'Steve';
    

    in a variable. How can do us?

    In PL/SQL?

    declare
      ln_sal sal.salary%type;
    begin
      ......
      select nvl(max(salary),0)
      into ln_sal
      from sal where name = 'Steve';
      .....
    end;
    

    Published by: JAC on 19 Sep 2012 09:16
    Assuming that there are no null values in the column SALARY...

  • Aggregate functions

    Why aggregate (sum, min) function returns a line (with a null value) even though my there is no lines satisfying the where clause. Pls find the below example

    Select 1 from scott.emp where deptno = 1,011 - no row returned

    Select sum (sal) in the scott.emp where deptno = 1,011 - returns a row with null as a value. The thing is that if there are no rows in the emp table with a floating point number of employee. (Sum) should not have been called.

    Here's how that works:

    http://docs.Oracle.com/CD/E11882_01/server.112/e26088/functions003.htm

    All functions except aggregation COUNT (*), GROUPING , and GROUPING_ID ignore NULL values. You can use the NVL function in the argument of an aggregation function to substitute a value for a null value COUNT and REGR_COUNT never return null, but returns a number or zero. For all the remaining functions of aggregation, If the DataSet has no rows, or contains only rows with NULL values as arguments to the aggregate function, then the function returns the value null.

  • Bug with the aggregate function and no group

    When I run the following query:
    with the_table as
    (
      select 1 as id, 100 as cost from dual
      union all select 2 as id, 200 as cost from dual
      union all select 3 as id, 300 as cost from dual
      union all select 4 as id, 400 as cost from dual
      union all select 5 as id, 500 as cost from dual
    )
    select id, cost
    from
    (
      select id, cost
      from the_table
      --
      union all
      --
      select 0 as id, sum(cost) as cost
      from the_table
      where 0 = 1
      -- group by 1
    )
    order by id;
    I get this result:
    ID    COST
    --  ------
     0  <null>     
     1     100
     2     200
     3     300
     4     400
     5     500
    However, when I "uncomment" the line "Group 1", the query works as expected (without the id = rank 0).

    This occurs even when "the_table" is an array.

    Someone else comes through this (and if so, how long is a problem)?

    The database is 11.2.0.2.0 64-bit.

    EDIT: It also happens without a Union - the following returns a single line (with null 0 and cost of id) without the Group By and no line with her:
    select id, cost
    from
    (
      select 0 as id, sum(cost) as cost
      from 
      (
        select 1 as id, 100 as cost from dual
        union all select 2 as id, 200 as cost from dual
        union all select 3 as id, 300 as cost from dual
        union all select 4 as id, 400 as cost from dual
        union all select 5 as id, 500 as cost from dual
      )
      where 0 = 1
      -- group by 1
    )
    order by id
    Edited by: Donbot February 15, 2012 10:29

    Donbot wrote:
    Someone else comes through this (and if so, how long is a problem)?

    The database is 11.2.0.2.0 64-bit.

    This is a documented behavior.

    http://docs.Oracle.com/CD/E11882_01/server.112/e26088/functions003.htm#SQLRF20035

    "
    All except COUNT (*) GROUPING and GROUPING_ID aggregate functions ignore NULL values. You can use the NVL function in the argument of an aggregation function to substitute a value for a null value. COUNTY and REGR_COUNT never return null, but return a number or zero. For all remaining functions of aggregation, * if the DataSet contains no line, * or if it contains only the rows with NULL values as arguments to the aggregate function, * then the function returns null.*
    "

  • function Oracle detects not value space, or null

    I have a feature of oracle that take two id parameter, newClientLegalName. On the basis of the second function parameter performs a specific action. the specified condition is


    IF (newClientLegalName! = "BDU")) THEN

    -do something

    ON THE OTHER

    -do something

    END IF

    It works very well in all conditions except when I pass null or space in the second parameter. How do I create a condition that fetch the value null or space when it is passed as a second parameter. signature of the function is

    copyReview (caseID in NUMBERS, newClientLegalName IN Client.Legal_Name%TYPE)

    Thank you
    signhm

    Use the NVL function.

    for example

    IF (nvl (newClientLegalName, 'empty')! = "BDU")) THEN

    Published by: Robert Geier on October 14, 2009 16:02

  • Problem with NVL in Oracle 10 g version 10.2.0.1.0

    Hello

    I am facing a problem with NVL. I have two configurations one with version 10g 10.2.0.1.0, UAT and other with 10.2.0.3.0.
    I defined a function (SQL written below). Both configurations have the same defined function and I'm passing the same value/setting as well. But, I get different results on both configurations.
    Pls let me know the reasons... .to this strange behavior of function, I think it's because of the NVL

    Function
    CREATE OR REPLACE FUNCTION Fn_Dt_Get_amnt (NUMBER of N_EXP_COVERED_AMT,
    V_SRC_SYSTEM_ID varchar2,
    NUMBER OF N_UNDRAWN_AMT_LCY
    NUMBER OF N_EXP_LIMIT_NCY
    NUMBER OF N_CCF_PERCENT
    NUMBER OF N_EXP_AMT_LCY
    p_undrawn_amt varchar2)
    + RETURN VARCHAR2 AS +.

    + UNDRAWN_AMT NUMERIC (22.3). +

    BEGIN


    IF P_UNDRAWN_AMT IN ('EXP_COVERED')

    THEN
    IF (UPPER (V_SRC_SYSTEM_ID) IN ("UBS"))
    THEN
    IF (N_EXP_AMT_LCY (case when NVL(N_UNDRAWN_AMT_LCY,0) < 0 +))
    THEN 0
    of other NVL(N_UNDRAWN_AMT_LCY,0)
    (END) * NVL(N_CCF_PERCENT,0)) < N_EXP_COVERED_AMT
    THEN

    UNDRAWN_AMT: = (N_EXP_AMT_LCY (+)
    -case when NVL(N_UNDRAWN_AMT_LCY,0) < 0
    THEN 0
    OF OTHER NVL(N_UNDRAWN_AMT_LCY,0)
    (END) * NVL(N_CCF_PERCENT,0));
    ON THE OTHER
    UNDRAWN_AMT: = N_EXP_COVERED_AMT;
    END IF;


    ELSIF (UPPER (V_SRC_SYSTEM_ID) ("VP", "FW", "FN"))
    THEN

    IF NVL(N_EXP_AMT_LCY,0) (CASE WHEN (NVL(N_EXP_LIMIT_NCY,0)-NVL(N_EXP_AMT_LCY,0)) < 0 +)
    THEN 0
    ELSE (NVL(N_EXP_LIMIT_NCY,0)-NVL(N_EXP_AMT_LCY,0))
    END) * NVL(N_CCF_PERCENT,0) < N_EXP_COVERED_AMT


    THEN

    UNDRAWN_AMT: = (NVL(N_EXP_AMT_LCY,0)(+)
    CASE WHEN (NVL(N_EXP_LIMIT_NCY,0)-NVL(N_EXP_AMT_LCY,0)) < 0
    THEN 0
    ELSE (NVL(N_EXP_LIMIT_NCY,0)-NVL(N_EXP_AMT_LCY,0))
    (END) * NVL(N_CCF_PERCENT,0));

    ON THE OTHER

    UNDRAWN_AMT: = N_EXP_COVERED_AMT;
    END IF;

    END IF;


    ELSIF P_UNDRAWN_AMT IN ('NO')

    THEN
    IF UPPER (V_SRC_SYSTEM_ID) IN ("UBS")

    THEN

    UNDRAWN_AMT: = (CASE WHEN NVL(N_UNDRAWN_AMT_LCY,0) < 0
    THEN 0

    OF OTHER NVL(N_UNDRAWN_AMT_LCY,0)
    (END);

    ELSIF (V_SRC_SYSTEM_ID) UPPER IN ("VP", "FW", "FN")
    THEN
    UNDRAWN_AMT: = (CASE WHEN (NVL(N_EXP_LIMIT_NCY,0)-NVL(N_EXP_AMT_LCY,0)) < 0

    THEN 0
    ELSE (NVL(N_EXP_LIMIT_NCY,0)-NVL(N_EXP_AMT_LCY,0))
    (END);
    END IF;

    END IF;

    RETURN UNDRAWN_AMT;
    END Fn_Dt_Get_amnt;

    Setting run

    Oracle 10g 10.2.0.3.0
    SELECT FN_DT_GET_AMNT(1002872,'FW',699737.78,752154,0.5,52416.22,'EXP_COVERED')
    the double - power - 402285.11

    Oracle 10g 10.2.0.1.0
    SELECT FN_DT_GET_AMNT(1002872,'FW',699737.78,752154,0.5,52416.22,'EXP_COVERED')
    the double - power - 1002872

    I think the problem is with NVL, such that the code works fine with Coalesce on the two configurations. Can someone explain why?

    It is a bug, as evidenced by the test case I posted.
    Apparently when multiple NVLs are used with the CASE in the same expression.

    Works fine in the following versions:
    10.2.0.4
    10.2.0.3
    9.0.1.1.1

  • Function MAX to get the recent date does not work as expected

    Hello
    I recently started working on Oracle and came across this problem. I've coded below, given the query to obtain the most recent date in the table and compare with Sysdate + 1 timestamp. I am not able to understand what I coded wrong but the query gives me bad such results as explained below:

    Select NVL (MAX (to_char (max (last_update),' dd/mm/yyyy hh12:mi: ss AM')), to_char (sysdate + 1, ' dd/mm/yyyy') |') 12: 00')
    of Audit_Table_Name
    where col1 = 'AA '.
    and location_type = 'STATE '.
    and original_flag = 'Y '.
    Group of col1, location_type, original_flag;


    Here Max (last_update) of value Audit table contains--> 25/08/2009 12:00:00 AM
    Note: The last_update data type is Date
    If ideally, it should give 26/08/2009 12:00:00 AM, but surprisingly it gives 25/08/2009 12:00:00 AM as output. Can someone explain why this is happening and what to do to get the correct results?

    Kind regards
    Amol

    user11826653 wrote:
    Hello
    I recently started working on Oracle and came across this problem. I've coded below, given the query to obtain the most recent date in the table and compare with Sysdate + 1 timestamp. I am not able to understand what I coded wrong but the query gives me bad such results as explained below:

    Select NVL (MAX (to_char (max (last_update),' dd/mm/yyyy hh12:mi: ss AM')), to_char (sysdate + 1, ' dd/mm/yyyy') |') 12: 00')
    of Audit_Table_Name
    where col1 = 'AA '.
    and location_type = 'STATE '.
    and original_flag = 'Y '.
    Group of col1, location_type, original_flag;

    Here Max (last_update) of value Audit table contains--> 25/08/2009 12:00:00 AM
    Note: The last_update data type is Date
    If ideally, it should give 26/08/2009 12:00:00 AM, but surprisingly it gives 25/08/2009 12:00:00 AM as output. Can someone explain why this is happening and what to do to get the correct results?

    Kind regards
    Amol

    Your sysdate + 1 is the second half of your NVL function it will return only sysdate + 1 when max) last_update) is set to null. I think what you need is

    select to_char(greatest(max(last_update),trunc(sysdate)+1),'mm/dd/yyyy hh:mi:ss AM') 
    
  • NVL and select box so bad

    Hello

    I am struggling with the fact that my subquery returns null and the faulty update.
    Although the compilation does not throw an error, using this script in my client applicatio
    gives me ORA... can not to update to NULL or something (sorry, not having the error code available now).

    I tried with the nvl function and a SELCT HOLSTER... WHEN... THEN, but the two did not help.
    It would be great if someone has an idea.

    Thank you very much

    here is the code

    I noticed that oracle treats string empty "as null is why I introduced the dummy keyword 'empty '.

    Try

    UPDATE   users a
    SET      a.firstname =
               NVL ( (SELECT   propertyvalue
                      FROM     ads_properties b, ads_users c
                      WHERE    c.userid       = b.propertyuserid
                      AND      c.username     = a.username
                      AND      b.propertyname = 'givenName'
                      AND      b.propertyvalue IS NOT NULL
                      AND      ROWNUM = 1), 'NOTHING')
    WHERE    a.firstname IS NULL
    

Maybe you are looking for

  • Printing with color HP LaserJet 2840 PCL6 printer problem

    I installed the printer on another computer. When I try to print, every time I need to reset the printer before it prints a document that is made by the new installed MS Office 2010. What to do to be able to print at a time?

  • 0 x 80070571

    I am trying to burn a cd and get get this error. Help!

  • Integration of BBM: unable to complete the installation (in the Simulator)

    Hi, I have a problem, I wanted to integrate BBM to my Apps, but I actually just Simulator test. When I run and I add my blackberry ID I always get to this page: Isn't this possible? I have the newest emulator (10_0_09-2318) and the vmware player 4.0.

  • Lock screen image burn on the screen?

    I like the idea of the lock screen, but does not burn this image on the screen? I don't know how to explain it, but like when you allways see a slight "shadow", the contours of the image regardless of your practice on the computer.

  • Premiere Pro Panel SDK: Function exchanging assets (Offline/Online)

    HelloI'm trying to find out if the first API is able to exchange assets of the sequence/project for a workflow / offline.Consider that we have an available property management system that manages all derivatives (source, mezzanine and proxy) a video