Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. SQL Server
  3. Question
SQL Server

How to display sql server boolean type True & False for 1 & NULL ?

Asked by Caroline Brown Apr 22, 2021 5.0K views 2 answers
Share

About this question

In my query I am able to get column values as 1 and NULL. I would like to display True & False for 1 & NULL? /*Declare Variable*/ DECLARE @Pivot_Column [nvarchar](max); DECLARE @Query [nvarchar](max); /*Select Pivot Column*/ SELECT @Pivot_Column= COALESCE(@Pivot_Column+',','')+ QUOTENAME(PracticeLocationServiceCode) FROM (SELECT DISTINCT [PracticeLocationServiceCode] FROM PracticeLocationService)Tab /*Create Dynamic Query*/ SELECT @Query=' ;WITH ServiceOffered AS ( SELECT PracticeLocationID, '+@Pivot_Column+'FROM (SELECT PracticeLocationID, PracticeLocationServiceCode,CAST(ServiceFlag AS INT) ServiceFlag FROM PracticeLocationService )Tab1 PIVOT ( MAX(ServiceFlag) FOR [PracticeLocationServiceCode] IN ('+@Pivot_Column+')) AS Tab2 ) SELECT REGION,LocationShortName,GLCODE,C.* FROM ServiceOffered C INNER JOIN VWLOCATIONS V ON V.PracticeLocationID = C.PracticeLocationID ORDER BY REGION,LocationShortName ' /*Execute Query*/ EXEC sp_executesql @Query Does SQL Server have Boolean data type?


Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Jul 17, 2024

In SQL Server, the BIT data type is used to represent boolean values. A BIT can have a value of 0, 1, or NULL. To display TRUE and FALSE for 1 and NULL respectively, you can use the CASE statement to convert these values in your query.

Here’s an example query demonstrating how to do this:

Example Table:

Assume you have a table named ExampleTable with a column named IsActive of type BIT.

  CREATE TABLE ExampleTable (    ID INT PRIMARY KEY,    IsActive BIT);

  INSERT INTO ExampleTable (ID, IsActive) VALUES (1, 1), (2, NULL), (3, 0);

Query to Display TRUE and FALSE:

You can use the CASE statement to convert the BIT values to TRUE or FALSE.

  SELECT     ID,    CASE         WHEN IsActive = 1 THEN 'TRUE'        WHEN IsActive = 0 THEN 'FALSE'        ELSE 'FALSE'  -- Defaulting NULL to 'FALSE'    END AS IsActiveStatusFROM     ExampleTable;In this query:

If IsActive is 1, it displays TRUE.

If IsActive is 0 or NULL, it displays FALSE.

If you specifically want to distinguish between NULL and 0, you can modify the CASE statement accordingly:

  SELECT     ID,    CASE         WHEN IsActive = 1 THEN 'TRUE'        WHEN IsActive = 0 THEN 'FALSE'        WHEN IsActive IS NULL THEN 'FALSE'  -- Explicitly handling NULL    END AS IsActiveStatusFROM     ExampleTable;

In this version, you explicitly check for NULL values to ensure they are displayed as FALSE.

Displaying Custom Text:

If you need more customized text for TRUE and FALSE, simply replace 'TRUE' and 'FALSE' with your desired strings.

  SELECT     ID,    CASE         WHEN IsActive = 1 THEN 'Active'        WHEN IsActive = 0 THEN 'Inactive'        ELSE 'Inactive'  -- Defaulting NULL to 'Inactive'    END AS IsActiveStatusFROM     ExampleTable;

This approach ensures that your query result displays human-readable boolean values based on the BIT column in your SQL Server table.








Was this helpful?

More SQL Server discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest SQL Server Blogs

Guides, tips and career advice on SQL Server from JanBask experts.