Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9300099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:42:28+00:00 2026-06-18T22:42:28+00:00

I have a SQL query running on SQL Server 2012 that needs to compare

  • 0

I have a SQL query running on SQL Server 2012 that needs to compare a bit value and return a string if that value is 1 and an empty string if it is zero.

Originally I had it as a CASE statement like this:

CASE WHEN myBit = 0 THEN 
 -- do other comparisons etc to build up the return string.
 +'myString' 
ELSE 
-- do other comparisons etc to build up the return string.
'' END

The problem is that all of the code in ‘do other’ section is the same. All I want to do is append a string to the returned value if the bit is zero and nothing if it is 1.

So I refactored it to only have the common code once and then append to the string at the end like this:

-- do other comparisons etc to build up the return string. +
ISNULL(NULLIF(Cast(ISNULL(CAST(NULLIF(myBit, 0) AS NVARCHAR), 'myString') AS varchar),'0'),'')

However the above seems very messy not least because of the CAST statements required.

I’m looking for a clean and neat way of doing this but have run out of ideas – anyone have a better way of achieving this? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T22:42:30+00:00Added an answer on June 18, 2026 at 10:42 pm

    You can also use IIF and CONCAT as you are on SQL Server 2012.

    SELECT CONCAT('Start',
                  IIF(@mybit=0,'myString',''),
                  'End')
    

    IIF is a bit more concise than CASE. CONCAT might be beneficial in that it casts non string types to strings automatically and concatenating NULL is treated the same as concatenating an empty string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a long-running SQL Server 2005 query that I have been hoping to
I have a SQL Query running on SQL Server 2008 R2 that returns a
I have an SQL Query (For SQL Server 2008 R2) that takes a very
I have a SQL Server 2005 query that generates a large result set (up
I've got 2 servers running SQL Server 2008, and I have the following query:
Edit: using SQL Server 2005. I have a query that has to check whether
Assume that you have a running SQL Server Express instance named (local)\SQLEXPRESS. Its database
I have a T-SQL Query running on SQL Server 2005 where I have defined
i have sql query select * from Roles Join Users On Roles.Role=Users.RoleId it return
I have an SQL query like this: set @fromdate = '2012/01/01 00:00:00' set @todate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.