Return value in SQL Server query

1

I need this query to return 2 instead of A. TYPEUSUARY is of type varchar.

Can someone tell me where the error is?

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

        GO
    
asked by anonymous 21.10.2016 / 14:21

4 answers

2

The case usually comes before where no select .

Your wish should be like this;

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

        GO

So it works ois i case is not a comparative function but a conditional function for a given value.

    
21.10.2016 / 14:37
1
SELECT CASE TIPOUSUARIO 
          WHEN 'A' THEN 2 
          ELSE 9
          END FROM UP
  WHERE ...FILTRO....
    
21.10.2016 / 14:37
1

I do not know if the name of the field works in where the way you are doing, but try to put WHEN soon after case .

TIPOUSUARIO = case when TIPOUSUARIO = 'A' THEN '2' END.
    
21.10.2016 / 14:43
0

Your biggest mistake is in the incorrect use of CASE syntax .

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

Do not give too much understanding of what you are trying to do, but if you want the return to be 2 at the value log that is in TIPOUSUARIO , you can use its case in select as well. p>

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

Now if you use case in where would be.

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

this would work if there is TIPOUSUARIO = 'A' and TIPOUSUARIO = '2' .

    
21.10.2016 / 16:03