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 8119447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:48:26+00:00 2026-06-06T04:48:26+00:00

I am trying to do some text formatting with sql (MS SQL server 2008).

  • 0

I am trying to do some text formatting with sql (MS SQL server 2008). I have three variables which are either going to return empty strings or some sort of text, they will never be null. I would like the display the text in the following way:
@Var1=’A’ @Var2=’B’ @Var3=’C’ ==> A, B, C
@Var1=’A’ @Var2=” @Var3=’C’ ==> A, C
@Var1=” @Var2=’B’ @Var3=’C’ ==> B, C
@Var1=” @Var2=” @Var3=’C’ ==> C
.
.
.
@Var1=” @Var2=” @Var3=” ==>
etc

Here is a simplified version of what I have…

DECLARE @Var1 NVARCHAR(100)
DECLARE @Var2 NVARCHAR(100)
DECLARE @Var3 NVARCHAR(100)
SET @Var1 = 'A'
SET @Var2 = 'B'
SET @Var3 = 'C'
SELECT 
ISNULL(NULLIF(@Var1,''), '') 
    + 
        CASE
            WHEN NULLIF(@Var1,'') IS NOT NULL AND NULLIF(@Var2,'') IS NOT NULL THEN ', ' ELSE '' 
        END 
    +
ISNULL(NULLIF(@Var2,''),'')
    +
        CASE
            WHEN NULLIF(@Var2,'') IS NOT NULL AND NULLIF(@Var3,'') IS NOT NULL THEN ', ' ELSE '' 
        END 
    +
ISNULL(NULLIF(@Var3,''),'')

I feel like I am missing some important details. Let me know if there is any clarification needed.

  • 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-06T04:48:27+00:00Added an answer on June 6, 2026 at 4:48 am

    Try:

    select  case 
            when str is null then ''
            else left(str, len(str)-1) -- Remove last comma
            end
    from    (
            select  case when len(@Var1) > 0 then @Var1 + ', ' else '' end + 
                    case when len(@Var2) > 0 then @Var2 + ', ' else '' end + 
                    case when len(@Var3) > 0 then @Var3 + ', ' else '' end as str
            ) as SubQueryAlias
    

    Or with a reverse trick to avoid the subquery:

    select  reverse(stuff(reverse(
            case when len(@Var1) > 0 then @Var1 + ', ' else '' end +
            case when len(@Var2) > 0 then @Var2 + ', ' else '' end +
            case when len(@Var3) > 0 then @Var3 + ', ' else '' end
            ), 1, 2, ''));
    

    The key point here is that SQL Server is not very good at text formatting 🙂 You’re infinitely better off in a client side language like C#.

    Live example at SQL Fiddle.

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

Sidebar

Related Questions

I am trying to insert some text data into a table in SQL Server
I'm trying to import some data from Excel to SQL Server, I have a
I am trying to have some text towards the end of a hr tag
i was trying to do some simple text formatting using JEditorPane but then as
I'm trying to remove wiki formatting from some text so it can be parsed.
Am trying to find some text only if it contains english letters and numbers
I am trying to write some text over my picturebox so I thought the
I am trying to insert some text into <textarea> by query but it is
I'm trying to add some text to a listBox in a form, from another
I'm trying to make some text bold using HTML, but I'm struggling to get

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.