How to use CASE in an update ?

I did so and an error happened:
UPDATE TB_FUNCIONARIO SET NIVEL =
CASE WHEN CARGO = 'PROGRAMADOR' AND SALARIO BETWEEN 4000 AND 8000 THEN 'PLENO'
WHEN CARGO = 'PROGRAMADOR' AND SALARIO <= 2000 THEN 'JUNIOR'
ELSE 'SENIOR' END
how to solve ?
Edited by: 995757 on 23/03/2013 13:29

Ok, Solved

Similar Messages

  • How to use case function in where clause

    Hi,
    Suppose a table DEMO has columns
    DEMO TABLE
    user_id
    user_name
    location
    In this table i have 15 users. but out of 15 users i want to use only 5 users for passing as user_name.
    then how to achieve the result
    1. when i pass the particular 5 user_name in where clause then i should get all the user_name and for other 10 users it will show only the passing user_name.
    how to use case function

    Do you mean this ?
    SQL> var name varchar2(10)
    SQL> exec :name := 'ALLEN'
    PL/SQL procedure successfully completed.
    SQL> select ename from emp where case when :name in ('SMITH','ALLEN') then ename
      2  else :name end = ename;
    ENAME
    SMITH
    ALLEN
    WARD
    JONES
    MARTIN
    BLAKE
    CLARK
    SCOTT
    KING
    TURNER
    ADAMS
    JAMES
    FORD
    MILLER
    14 rows selected.
    SQL> exec :name := 'SMITH'
    PL/SQL procedure successfully completed.
    SQL> select ename from emp where case when :name in ('SMITH','ALLEN') then ename
      2  else :name end = ename;
    ENAME
    SMITH
    ALLEN
    WARD
    JONES
    MARTIN
    BLAKE
    CLARK
    SCOTT
    KING
    TURNER
    ADAMS
    JAMES
    FORD
    MILLER
    14 rows selected.
    SQL> exec :name := 'BLAKE'
    PL/SQL procedure successfully completed.
    SQL> select ename from emp where case when :name in ('SMITH','ALLEN') then ename
      2  else :name end = ename;
    ENAME
    BLAKERgds.

  • How to use CASE statement in WDA

    Hi All,
       Can any one Please expain me ' How to use CASE statement in Web dynpro ABAP? '
      Please give me an example also.
    Thanks in Advance !

    Hi,
    The usage of case statement in webdynpro is same as used in general ABAP.
    Data: l_id type string.
    l_id = wdevent->get_string( 'ID' ).
    case l_id.
    when 'BTN1'.
    when 'BTN2'.
    when 'OTHERS'.
    endcase.
    Regards,
    Radhika.

  • How to use CASE stmt in Where clause

    Hi,
    How to use CASE stmt in WHERE clause?. I need the code. Please send me as early as possible..........

    Hi,
    1004977 wrote:
    Hi,
    How to use CASE stmt in WHERE clause?. There's no difference between how a CASE expression is used in a WHERE clause, and how it is used in any other clause. CASE ... END always returns a single scalar value, and it can be used almost anywere a single scalar expression (such as a column, a literal, a single-row function, a + operation, ...) can be used.
    CASE expressions aren't needed in WHERE clauses very much. The reason CASE expressions are so useful is that they allow you to do IF-THEN-ELSE logic anywhere in SQL. The WHERE clause already allows you to do IF-THEN-ELSE logic, usually in a more convenient way.
    I need the code.So do the peple who want t help you. Post a query where you'd like to use a CASE expression in the WHERE clause. Post CREATE TABLE and INSERT statements for any tables used unless they are commonly available, such as scott.emp). Also, post the results you want from that sample data, and explain how you get those resuts from that data.
    See the forum FAQ {message:id=9360002}
    Please send me as early as possible..........As mentioned bfore, that's rude. It's also self-defeating. Nobody will refuse to help you because you don't appear pushy enough, but some people will refuse to help you if you appear too pushy.

  • How to use case when function to calculate time ?

    Dear All,
    May i know how to use case when function to calculate the time ?
    for the example , if the First_EP_scan_time is 12.30,  then must minus 30 min.  
    CASE WHEN FIRSTSCAN.EP_SHIFT <> 'R1' AND FIRSTSCAN.EP_SHIFT <> 'R2'
    THEN ROUND(CAST((DATEDIFF(MINUTE,CAST(STUFF(STUFF((CASE WHEN SHIFTCAL.EP_SHIFT = 'N1'
    THEN CONVERT(VARCHAR(8),DATEADD(DAY,+1,LEFT(FIRSTSCAN.EP_SCAN_DATE ,8)),112) + ' ' + REPLACE(CONVERT(VARCHAR(8),DATEADD(HOUR,+0,SHIFTDESC.EP_SHIFT_TIMETO + ':00'),108),':','')
    ELSE LEFT(FIRSTSCAN.EP_SCAN_DATE ,8) + ' ' + REPLACE(CONVERT(VARCHAR(8),DATEADD(HOUR,+0,SHIFTDESC.EP_SHIFT_TIMETO + ':00'),108),':','') END),12,0,':'),15,0,':') AS DATETIME),CAST(STUFF(STUFF(LASTSCAN.EP_SCAN_DATE,12,0,':'),15,0,':') AS DATETIME)) / 60.0 - 0.25) AS FLOAT),2)
    ELSE ROUND(CAST((DATEDIFF(MINUTE,CAST(STUFF(STUFF(FIRSTSCAN.EP_SCAN_DATE,12,0,':'),15,0,':') AS DATETIME),CAST(STUFF(STUFF(LASTSCAN.EP_SCAN_DATE,12,0,':'),15,0,':') AS DATETIME)) / 60.0) AS FLOAT),2) END AS OTWORK_HOUR

    Do not use computations in a declarative language.  This is SQL and not COBOL.
    Use a table of time slots set to one more decimal second of precision than your data. You can now use temporal math to add it to a DATE to TIME(1) get a full DATETIME2(0). Here is the basic skeleton. 
    CREATE TABLE Timeslots
    (slot_start_time TIME(1) NOT NULL PRIMARY KEY,
     slot_end_time TIME(1) NOT NULL,
     CHECK (start_time < end_time));
    INSERT INTO Timeslots  --15 min intervals 
    VALUES ('00:00:00.0', '00:14:59.9'),
    ('00:15:00.0', '00:29:59.9'),
    ('00:30:00.0', '00:44:59.9'),
    ('00:45:00.0', '01:00:59.9'), 
    ('23:45:00.0', '23:59:59.9'); 
    Here is the basic query for rounding down to a time slot. 
    SELECT CAST (@in_timestamp AS DATE), T.start_time
      FROM Timeslots AS T
     WHERE CAST (@in_timestamp AS TIME)
           BETWEEN T.slot_start_time 
               AND T.slot_end_time;
    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
    in Sets / Trees and Hierarchies in SQL

  • How to use CASE or DECODE in this?

    Hi , I have a query like this
    AND ...smthng
    AND --smtg
    AND age BETWEEN ....For the last AND clause for age column, I have the values passed from IN param of this SP in form of strings
    '0-30', '31-60', '61-90', and '91+'. The IN param is of course VARCHAR2.
    What I want to do in this AND clause
    e.g. for age range '0-30', the AND clause should be
    AND age BETWEEN ( if this range is '0-30' then ) 0 AND 30 ( if range is '31-60') then BETWEEN 31 AND 60.. and so on...
    However this BETWEEN will work till 61-90 range but for 91+ , there is no upper bound so it should be
    AND age>91
    Now , i am not sure how to achieve this both BETWEEN and '>' clauses in the same AND statement.
    DECODE will not work and am not sure how to use the CASE in this situation as even that can not solve this issue.
    Dont want to make the last AND clause dynamic
    Please suggest me how to do this.
    Thanks,
    Aashish
    Edited by: Aashish S. on Oct 21, 2011 6:01 PM

    A third alternative would be to choose a suitably large value as a high end for the 91+ group and use something like:
    age between to_number(CASE WHEN instr(:param, '+') = 0
                                  THEN substr(:param, 1, instr(:param, '-') -1)
                                  ELSE substr(:param, 1, instr(:param, '+') -1) end) and
                to_number(CASE WHEN instr(:param, '+') = 0
                               THEN substr(:param, instr(:param, '-') +1)
                               ELSE '9999' END)John

  • How to use escape character in update statement.

    Hi All,
    I'm trying to update table using following sql update statement, but everytime it's asking me for the input due to the '&' value in below sql.
    UPDATE xyz_xyz
       SET NAME = 'ABC & PQR'
    WHERE ID = (SELECT ID
                   FROM abc_abc
                  WHERE NAME = 'C & PQR');Please let me know how to use escape character syntax or let me know if there is any alternative solution.
    Thanks,
    Vishwas

    Hi,
    By default, & marks a substitution variable name.
    If you're not using substitution variables in that statement (or, if this is in PL/SQL, in that entire package or procedure) then the easiest thing to do is just diable substitution variables; then & will be a normal character:
    SELECT  DEFINE  OFF
    UPDATE xyz_xyz
       SET NAME = 'ABC & PQR'
    WHERE ID = (SELECT ID
                   FROM abc_abc
                  WHERE NAME = 'C & PQR');
    SET  DEFINE  ONIf you can't do that, then & is always taken literally if it comes right before a single-quote, so you could say:
    UPDATE xyz_xyz
       SET NAME = 'ABC &' || ' PQR'
    WHERE ID = (SELECT ID
                   FROM abc_abc
                  WHERE NAME = 'C &' || ' PQR');There is a SQL*Plus "SET ESCAPE" command, too, but if you use it, you have to worry about whether the escape character is to be taken literally or not.
    SET   ESCAPE  \Yet another alternative is to make some other character, such as ~, mark the substitution variables:
    SET  DEFINE  ~Read all about them in the SQL*Plus manual.
    http://download.oracle.com/docs/cd/B28359_01/server.111/b31189/ch2.htm#sthref103

  • How to use case when in Select qry?

    Hi Friends,
    I want to use Case when in Select qry, my situation is like this
    SELECT bmatnr blgort bj_3asiz bmat_kdauf b~mat_kdpos
           SUM( ( case when bshkzg = 'S' then bmenge else 0 END ) -
            ( case when bshkzg = 'H' then bmenge else 0 END ) ) AS qty
           INTO corresponding fields of table it_projsal
        FROM mseg AS b
            INNER JOIN mkpf AS a ON bmblnr = amblnr
                                AND bmandt = amandt
        WHERE abudat  < '20061201' AND blgort IN ('1050')
          and b~mandt = '350'
        GROUP BY bmatnr bj_3asiz bmat_kdauf bmat_kdpos b~lgor
    If we give like this it gives an error.
    Please help me, how to use or handle in select qry itself.
    Regards
    Shankar

    this is not a way to select data from the DB tables.
    first get all the data from the DB tables then u have to do SUM Ups .
    Regards
    prabhu

  • How to use, Case function and Filter in Column Formula?

    Hello All,
    I am using case function and also would like to filter value to populate.
    Below is showing error :
    case
    when '@{Time}' = 'Year' then "Time"."Fiscal Year"
    when '@{Time}' = 'Quarter' then "Time"."Fiscal Quarter"
    when '@{Time}' = 'Month' then FILTER ("Time"."Fiscal Period" USING "Time"."Fiscal Period" NOT LIKE 'A%')
    else ifnull('@{Time}','Selection Failed') end
    Thanks, AK

    when '@{Time}' = 'Month' then FILTER ("Time"."Fiscal Period" USING "Time"."Fiscal Period" NOT LIKE 'A%')I dont think Filter this works here or any other data types except number.
    Try to use option Column's->Filter->Advanced->Convert this filter to SQL
    If helps mark

  • How to use case when statements in ODI

    I need to put conditional logic before DVM look up in ODI. In the expression editor I put the following statement:-
    CASE WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='RCS' THEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME=EBIZ_CELL.CELL_DATA
    WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='FDC' THEN CONCAT(POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME,POC_JOURNAL_TEMP_SOURCE_TBL.BOOK_CODE)=EBIZ_CELL.CELL_DATA
    END
    It did not work,
    Under Operators ->All Executions, found the error:-
    905 : 42000 : java.sql.SQLException: ORA-00905: missing keyword
    The description in Session Task contained:-
    select     
         C1_JOURNAL_TEMPL
    from     APPS.POC_JOURNAL_TEMP_SOURCE_TBL POC_JOURNAL_TEMP_SOURCE_TBL, APPS.C$_0POC_JOURNAL_TEMP_TARGET_TB
    where     
         (1=1)
    And (CASE
    WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='RCS'
    THEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME=C2_CELL_DATA
    WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='FDC'
    THEN CONCAT(POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME,POC_JOURNAL_TEMP_SOURCE_TBL.BOOK_CODE)=C2_CELL_DATA
    END)
    Checked the above code in PL/SQL Developer but it gave errors.
    In PL/SQL developer tried to check a simple query using case-when but even that is giving errors in the case-when portion.
    The query is as follows:-
    select phase_code, accounting_period, sum(eff_cc) as BD_Eff_QTD
    from prj_detail
    where
    case POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME
    when 'EXT'
    then
    1
    when 'RAC'
    then
    2
    when 'XXX'
    then
    3
    else
    end
    I would like to know what is wrong with the above code.
    Please let me know what is the correct way of using case-when in PL/SQL as well as in ODI.

    Your ODI case statement and PL/SQL Case statement both looks confusing.
    You are writing case statement under where clause which is not a good practise. If you want to implement logic like-
    select a,b,c from <table>
    where
    when cond^n^ 1 then do 1
    when cond^n^ 2 then do 2
    then better you seperate your query for each filter and do a union, in other words-
    select a,b,c from <table>
    where cond^n^ 1
    union
    select a,b,c from <table>
    where cond^n^ 2
    If you are writing case staement to retrieve a value/column (EBIZ_CELL.CELL_DATA) then no need to include it under filter.
    ODI case for column EBIZ_CELL.CELL_DATA will be:
    CASE
    WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='RCS'
    THEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME
    WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='FDC'
    THEN CONCAT(POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME,POC_JOURNAL_TEMP_SOURCE_TBL.BOOK_CODE)
    END
    Pl/SQL query-
    select phase_code, accounting_period,
    case POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME
    when 'EXT'then 1
    when 'RAC' then 2
    when 'XXX' then 3
    else 'default value' --- (if no else needed then can also remove else part)
    end,
    sum(eff_cc) as BD_Eff_QTD
    from prj_detail
    Suggested as per what is understood, hope it helps.
    Edited by: 939451 on Jul 5, 2012 12:47 AM
    Edited by: 939451 on Jul 5, 2012 12:48 AM

  • How to use case to replace the & in select statement

    Oracle version : 11.1.0.6.0
    RHEL
    Hi,
    I want to get a list of all expired and locked users but want to replace the "&" with and here is my code but it isn't working.
    Please some guidance, thanks.
    SQL> select username,
    2 case (account_status like 'EXP%' then 'EXPIRED AND LOCKED'
    3 ELS 'no locked users'
    4 end) account_status
    5 from dba_users
    6 where account_status like 'EXPI%';
    case (account_status like 'EXP%' then 'EXPIRED AND LOCKED'
    ERROR at line 2:
    ORA-00907: missing right parenthesis

    798188 wrote:
    thanks for the comments I corrected the ELSE and 'EPX%' but still same error :
    EOH> select username,
    2 case (account_status like 'EXP%' then 'EXPIRED AND LOCKED'
    3 ELSE 'no locked users'
    4 end) account_status
    5 from dba_users
    6 where account_status like 'EXP%';
    case (account_status like 'EXP%' then 'EXPIRED AND LOCKED'
    ERROR at line 2:
    ORA-00907: missing right parenthesisFirst of all, you do not need a CASE statement.
    Did you see the where clause of your query??
    6 where account_status like 'EXP%';do you realise that your query will always return those records for which value of account_status column starts with 'EXP'?
    Why use CASE when it could be simply written like this :
    SELECT username,
           'EXPIRED AND LOCKED'
    FROM   dba_users
    WHERE  account_status LIKE 'EXP%'

  • How to use CASE with UPDATE?

    Hi all. I'm trying to update multiple rows in a MS-Access
    database using a single query, with different values for each row.
    Here's a simplified example of the code:
    <cfquery name="JustInCase" datasource="variables.DB">
    Update Users
    SET First_Name =
    CASE WHEN ID = 1 THEN 'Nini' END,
    SET Last_Name
    CASE WHEN ID = 32 THEN 'Yumpi' END
    </cfquery>
    Trying to run something like this produces the following
    error message:
    [Macromedia][SequeLink JDBC Driver][ODBC
    Socket][Microsoft][ODBC Microsoft Access Driver] Syntax error
    (missing operator) in query expression 'CASE WHEN ID = 1 THEN
    'Nini' END'.
    I realize this can easily be done using multiple queries, but
    the actual code will contain many updates, and I would like to at
    least test the possibility of improving performance using a single
    query instead of multiple queries. I would greatly appreciate any
    help with this - I've been trying all kinds of variations, and
    nothing seems to satisfy emperor ColdFusion. Any ideas?

    Case might be a verity tag but it is also a valid sql keyword
    in many dbs. I use it in select clauses frequently and have seen it
    used in where clauses.
    The problem with the original query is that, it the value of
    id is not 1, this part
    SET First_Name =
    CASE WHEN ID = 1 THEN 'Nini' END,
    will cause a syntax error. For those values of 1 and 32, this
    syntax will run, but will update every row in the table which might
    not be the plan.
    update users
    set
    case when id = 1 then first_name = 'Nini'
    when id = 32 then last_name = 'Yumpi'
    else first_name = first_name
    end
    ranger, your code will also fail if the value is neither 1
    nor 32. You need a default case.

  • How to use multipule join in update stmt

    hi,
    i have a query in mssql i want to translate it using oracle 11g r2 latest sysntex
    UPDATE trips
    SET locations = trips.city + ', ' + poi.city
    FROM trips INNER JOIN poi
    ON poi.trip_guid = trips.guid
    where trips.trip_guid =1
    UPDATE trips t
    SET locations = t.city + ', ' + p.city
    FROM poi p
    ON p.trip_guid = t.guid
    where t.trip_guid =1
    please tel me how i can write it in oracel 11g r2
    i also show there is mearge statmet is it sql or plsql
    yours sincerely

    update trips t
    set locations = t.city || ', ' ||
    (select p.city || e.allow
    from poi p
    , emp e
    where p.trip_guid = t.guid AND e.id = t.id
    update trips t
    set locations = t.city || ', ' ||
    (select p.city || e.allow
    from poi p
    left join emp e on e.id = t.id
    where p.trip_guid = t.guid
    1)are these both query same and correct if i want to upate all rows.
    2) some body said following two query are different
    if there is any theory then pls tel me. because i have to use rownum<2 to get the top most row rather than distinct
    in few cases
    at some other places.
    -- works but look at the number of rows updated
    UPDATE test t
    SET (tablespace_name, extent_management) = (
    SELECT DISTINCT u.tablespace_name, u.extent_management
    FROM user_tables a, user_tablespaces u
    WHERE t.table_name = a.table_name
    AND a.tablespace_name = u.tablespace_name
    AND t.table_name LIKE '%A%');
    -- works properly
    UPDATE test t
    SET (tablespace_name, extent_management) = (
    SELECT DISTINCT (u.tablespace_name, u.extent_management)
    FROM user_tables a, user_tablespaces u
    WHERE t.table_name = a.table_name
    AND a.tablespace_name = u.tablespace_name)
    WHERE t.table_name LIKE '%A%';
    yours sincerely
    Edited by: 944768 on Feb 20, 2013 5:36 AM
    Edited by: 944768 on Feb 20, 2013 5:39 AM

  • How to use one form to update two tables

    How can I do that? HTMLDB wizard or form on table doesn't give me an option to use more than one table in a form or I don't know about it. I created new process which redirects the form to another page after submitting the form. On the second page I created new process which uses the same variables from the previous form page. This process runs on page load before header but it is just not working right.
    So, what is the proper way to update two tables with the same form fields?

    Hello Vikas,
    "The Automatic Row Fetch and Automatic DML processes are a pair, you can't have one without the other."
    Are you sure about that? I have a page, which populate some of the items from TableA, using manual select statement, and after the user input, save some of it in TableB, using Automatic DML. No ARF in this process and it seems to work just fine. Come to think of it, what about a simple form, populated entirely by the user input, and then being saved to the db, using Automatic DML? No ARF here also.
    For the problem in hand, if you can't have more then one Automatic DML per page, I think that the simplest solution will be to define a pl/sql process, with two INSERT statement to the two different tables.
    Regards,
    Arie.

  • How to use BAPI extension for updating field which is not in BAPI stracture

    I am doing a conversion for control cycle create. The data is maintained in DB Table "PKHD". i have to update 12 fields threre through BAPI "BAPI_KANBANCC_CREATE". there are 11 fields in BAPI structure. but 1 field called"BERKZ" is not there . How can i update it through EXTENSION.

    Hi ,
    in the bapi extension check one structure with name BAPIPAREX will available..
    you need to pass custom structure in that..
    ands conactenate 12 field of your structure and pass in to value1 in bapirex structure and append.
    go to se11 and enter >bapiparex> check where it is used -->see the zprogram and check how it is used the add your code according to that..
    Regards,
    Prabhudas

Maybe you are looking for