Doubt Case Sqlserver

Asked

Viewed 38 times

0

I’m using the following case:

 CASE WHEN RESPOSTA.CD_RESPOSTA = 1 THEN 'ATENDE'
      WHEN RESPOSTA.CD_RESPOSTA = 2 THEN 'NÃO ATENDE'
      ELSE 'NÃO APLICÁVEL' END AS DS_RESPOSTA

I have the value of ANSWER.CD_RESPOSTA: 2. The field is an Int, not null.

But it is always returning 'NOT APPLICABLE'. What I am doing wrong?

  • 1

    We would have to see the query where you are using. To answer this without seeing the full scenario would only be based on kick. If it is to kick: RESPOSTA can be null.

  • Are you using Azure or is local?

  • 1

    Your query is ok if it is only what you displayed. Check if you have values equal to 1 or 2 and if the where of your query is not wrong, see an example working: http://sqlfiddle.com/#! 18/90e9d/2

1 answer

0


Do a conversion treatment for the field as shown below. This way it is possible to guarantee which data exists at the origin of the data. Depending on the results, you can treat the origin of the data so that it is not necessary to convert.

 CASE 
  WHEN CONVERT(INT, ISNULL(RESPOSTA.CD_RESPOSTA, 0)) = 1 THEN 'ATENDE'
  WHEN CONVERT(INT, ISNULL(RESPOSTA.CD_RESPOSTA, 0)) = 2 THEN 'NÃO ATENDE'
  ELSE 'NÃO APLICÁVEL' END AS DS_RESPOSTA

Browser other questions tagged

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