case statement

Hemant Karnik 121 Reputation points
2021-12-23T12:27:43.927+00:00

s
select case AcctType

when in (43,41,47,85,80,81,66,44) then 'AOP'
when in (67,18,55,13,21,88,46,14,69,11,20,52,90,42,61,58,74,83,5,37,76,22,15,25,35,91,19,56) then 'Others'
when in (32,84,60,24,12) then 'Corporate'
when in (50,30,4,70,87,82) then 'Trust'
when in (8) then 'HUF'
when in (53) then 'NPO'
when in (71) then 'PEP'
else end from D009022

and the error is

Incorrect syntax near the keyword 'in'.

what wrong I am doing here?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,998 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,656 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Viorel 118K Reputation points
    2021-12-23T12:38:58.487+00:00

    Try another form:

    select case 
    when AcctType in (43,41,47,85,80,81,66,44) then 'AOP'
    when AcctType in (67,18,55, . . .) then 'Others'
    . . .
    

    To simplify this, maybe you can replace it with a join with special table.

    1 person found this answer helpful.
    0 comments No comments

  2. Hemant Karnik 121 Reputation points
    2021-12-23T12:44:36.563+00:00

    To simplify this, maybe you can replace it with a join with special table.

    HOW Sir?
    Please help


  3. Hemant Karnik 121 Reputation points
    2021-12-23T16:55:01.203+00:00

    hi all
    thanks for your response
    how do I close this thread?


  4. EchoLiu-MSFT 14,581 Reputation points
    2021-12-24T03:14:29.697+00:00

    Hi @Hemant Karnik

    Welcome to the microsoft TSQL Q&A forum!

    Your question is related to tsql, so my colleague helped you add the tsql tag.

    Your problem is that you are not familiar with the syntax of the CASE WHEN statement.Please refer to:

        select   
        case when AcctType in (43,41,47,85,80,81,66,44) then 'AOP'  
             when AcctType in (67,18,55,13,21,88,46,14,69,11,20,52,90,42,61,58,74,83,5,37,76,22,15,25,35,91,19,56) then 'Others'  
             when AcctType in (32,84,60,24,12) then 'Corporate'  
             when AcctType in (50,30,4,70,87,82) then 'Trust'  
             when AcctType in (8) then 'HUF'  
             when AcctType in (53) then 'NPO'  
             when AcctType in (71) then 'PEP'else null end   
        from D009022  
    

    There is also an error near your else statement, you can directly omit the ELSE sentence (in this case, the default ELSE NULL), or write the result you want after the ELSE. But you cannot write ELSE END.

    Please refer to the two ways of writing CASE WHEN:

        case sex   
           when 1 then 'male'  
           when 2 then 'female'  
           else 'null' end  
          
        case  
        when sex =1 then 'male'  
        when sex = 2 then 'female'  
        else 'null' end  
    

    For more details, please refer to:
    CASE (Transact-SQL)

    Regards,
    Echo


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.