Problem with CASE WHEN SQL-SERVER

1
SELECT CASE WHEN VALOR > 0 THEN
VALOR ELSE
"Não tem"
END AS NOMECOLUNA
FROM BLABLABLA

I'm having an error where it's not possible to convert "does not have" to int.

The issue is that I wanted it to return written that it does not have when the integer is 0 ...

    
asked by anonymous 27.04.2018 / 14:17

2 answers

1

You can convert numeric to VARCHAR since all results of CASE must have the same type:

SELECT CASE
         WHEN VALOR > 0 THEN CAST(VALOR AS VARCHAR)
         ELSE 'Não tem'
       END AS NOMECOLUNA
  FROM BLABLABLA
    
27.04.2018 / 14:38
1
Within the Case in SQL it is not possible to generate a column with numeric and text values as a single answer.

That is, in your code if the VALUE is greater than 0 it generates a numeric result and if it is less than 0 it generates a text value.

I thought of 2 possible solutions:

1- Generate a separate column of value:

SELECT 
VALOR,
CASE WHEN VALOR > 0 THEN
"Com Valor" ELSE
"Não tem"
END AS NOMECOLUNA
FROM BLABLABLA

2- Generate the result with text values for all cases (transform number into text):

SELECT CASE WHEN VALOR > 0 THEN
Put(VALOR, "numero de caracteres") ELSE
"Não tem"
END AS NOMECOLUNA
FROM BLABLABLA
    
27.04.2018 / 14:43