13

I am trying to convert a boolean to an integer. Here is the coding I have so far:

CONVERT(int, column_name) AS ALIAS

The query runs without an error. However, it is not producing the results I want.

What do I need to do?

5
  • I should note that it does convert the values to integers. However, it only returns 0s. Why is this happening? Commented Jun 10, 2014 at 21:28
  • Why do you need a boolean to be an integer? they're already effectually only 1 or 0. Why not just check for true or false? Commented Jun 10, 2014 at 21:34
  • Maybe all the records have false in that field. What is the rdbms? That's relevant. Commented Jun 10, 2014 at 21:34
  • you mean you converting BIT to integer? Can u show sample data from "column_name" ? Commented Jun 10, 2014 at 21:40
  • I need to convert it to an int because I want to aggregate it. I'm using SQL Server 2012 Commented Jun 10, 2014 at 21:40

1 Answer 1

17

What you are doing should be enough to convert BIT values to int as shown below.

DECLARE @TABLE TABLE(Value BIT)

INSERT INTO @TABLE VALUES 
(1),(0),(1),(0),(1),(0),(1)

Query

SELECT  Value
       ,CAST(Value AS INT)  AS Casted
       ,CONVERT(int, Value) AS Converted
FROM @TABLE 

Result Set

╔═══════╦════════╦═══════════╗
║ Value ║ Casted ║ Converted ║
╠═══════╬════════╬═══════════╣
║     1 ║      1 ║         1 ║
║     0 ║      0 ║         0 ║
║     1 ║      1 ║         1 ║
║     0 ║      0 ║         0 ║
║     1 ║      1 ║         1 ║
║     0 ║      0 ║         0 ║
║     1 ║      1 ║         1 ║
╚═══════╩════════╩═══════════╝
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.