6

Is there a way to return an int value for a boolean field in a sql select statement?

David Gauer
  • 61
  • 1
  • 1
  • 2
  • 1
    Do you mean a "bit" field? SQL Server has no boolean. And can you give some more background please so we understand the context. – gbn Sep 21 '11 at 15:23

4 Answers4

6
declare @b bit = 1

select convert (int, @b)

or

select cast(@b as int)

more

garik
  • 6,722
  • 10
  • 43
  • 56
2

If that is really a boolean column, you can do this:

SELECT case 
          when boolean_value then 1
          else 0 
       end as boolean_as_integer
FROM your_table

that way you can also return other values than 1/0 if you need to.

1

In SQL Server, you can use CAST()

SELECT CAST(Bit_Column TO INT)

but it won't work in MySQL. For MySQL, you can use

SELECT IF(Bit_Column, 1, 0)
1

I think this is not SQL Server. Maybe PostgreSQL? Try this

create table booln1(a  boolean);
insert into booln1 values(true),(false);
select a, cast(a as int) as a1 from booln1;
msi77
  • 1,135
  • 1
  • 7
  • 9