Case in sql query

2

I'm doing a query in sqlsrv 2000, to several tables where there are two fields estado and estado2 (that do the same thing, I do not know why they did it that way) that they keep 0 and 1 .

I currently have a case in that part of the query so that when state is equal 0, then estado='NO' , and if it is 1 then estado='SI' . I JUST ASK QUESTION FOR THE FIELD estado , and I need to ask if at least one of the two has a value of 1 or 0 without adding a new column to the query.

This is the part of the query that I have:

CASE a.estado
    WHEN 1 THEN 'SI'
    WHEN 0 THEN 'NO'
END AS Estado

I hope you understand me.

    
asked by daniel 22.06.2016 в 14:57
source

2 answers

3

You can use or without problems:

CASE 
    WHEN a.estado = 1 or a.estado2 = 1 THEN 'SI' 
    ELSE 'NO' 
END AS Estado 
    
answered by 22.06.2016 в 15:03
1

Another way to solve Query is by using the function; if as follows:

SELECT IF(a.estado = 1, IF(a.estado2 = 1, 'SI','NO'),'NO') AS estado FROM tu_tabla

With this query you should print YES or NO as the case may be.

Edit by Comment

Taking into account the colleague's comment Miquel Coll , I'm going to simplify the query, leaving it like this:

SELECT IF(estado = 1 OR estado2 = 1, 'SI','NO') AS estado FROM tu_tabla
    
answered by 22.06.2016 в 16:27