Return value in SQL Server query

Asked

Viewed 594 times

1

I need this appointment to return 2 instead of A. TIPOUSUARIO is the varchar type.

someone can tell me where the mistake is?

    USE DATABASE
    SELECT TIPOUSUARIO 
        FROM TABLE UP (NOLOCK) 
        WHERE 
             TIPOUSUARIO = case TIPOUSUARIO when 'A' THEN '2' END

        GO

4 answers

2


The case usually comes before the where la no select.

Your wish should be like this;

USE DATABASE
    SELECT case TIPOUSUARIO when 'A' THEN '2' END
        FROM TABLE UP (NOLOCK) 

        GO

Thus it works that i case is not a comparative function but a conditional function for a given value.

  • worked that way, thank you!

  • show , marks as completed then hugs

1

SELECT CASE TIPOUSUARIO 
          WHEN 'A' THEN 2 
          ELSE 9
          END FROM UP
  WHERE ...FILTRO....

1

Dude I don’t know if the name of the field works on where the way you’re doing, but try to put the WHEN shortly after the case.

TIPOUSUARIO = case when TIPOUSUARIO = 'A' THEN '2' END.

0

Your major error is in incorrect use of CASE syntax.

CASE input_expression   
     WHEN when_expression THEN result_expression [ ...n ]   
     [ ELSE else_result_expression ]   
END 

Don’t give to understand much what you are trying to do, but if you want the return to be 2 in the login of the value that is in TIPOUSUARIO, you can use your case in the select thus.

USE DATABASE
SELECT case when TIPOUSUARIO = 'A' THEN '2' END 
    FROM TABLE UP (NOLOCK)        
    GO

Now if you want to use the case in the where would be.

USE DATABASE
SELECT TIPOUSUARIO
    FROM TABLE UP (NOLOCK)  
     WHERE 
         TIPOUSUARIO = case when TIPOUSUARIO = 'A' THEN '2' END      
    GO

That would work if there was TIPOUSUARIO = 'A' and TIPOUSUARIO = '2'.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.