Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select true if more then 0 in t-sql

I would like to perform query in which as a result I have column with false if the value in former column is 0 and true if is greater then 0:

as example:

id  count
1   1
2   3
3   0
4   5
5   2

result:

id   count
1    true
2    true
3    false
4    true
5    true
like image 415
gruber Avatar asked Jun 26 '11 12:06

gruber


People also ask

How use multiple IF condition in SQL query?

If you are checking conditions against multiple variables then you would have to go for multiple IF Statements, Each block of code will be executed independently from other blocks. ELSE IF(@ID IS NOT NULL AND @ID in (SELECT ID FROM Places)) -- Outer Most Block ELSE IF BEGIN SELECT @MyName = Name ... ...

How do you select True False in SQL?

Answers. True or False are not reserved in SQL. Most often when programming you would use a bit and 0 = False while 1 = True.

How do I select greater than in SQL?

You can use the > operator in SQL to test for an expression greater than. In this example, the SELECT statement would return all rows from the customers table where the customer_id is greater than 6000.


2 Answers

select 
    id, 
    case 
        when count > 0 then 'true'
        else 'false'
    end as count
from myTable
like image 193
Petar Ivanov Avatar answered Oct 23 '22 15:10

Petar Ivanov


select id
    , case when count > 0 then cast(1 as bit) else cast(0 as bit) end as count
from myTable
like image 32
Kirill Polishchuk Avatar answered Oct 23 '22 16:10

Kirill Polishchuk