Is there a way to return an int value for a boolean field in a sql select statement?
Asked
Active
Viewed 3.9k times
6
-
1Do 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 Answers
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)
Paresh_Wadhwani
- 11
- 1
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