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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:55:19+00:00 2026-05-29T08:55:19+00:00

I am dynamically building a MySQL concatenation with multiple fields and different separators. I’d

  • 0

I am dynamically building a MySQL concatenation with multiple fields and different separators. I’d like to drop the separator if the currently concatenated field is blank or null.

$concat_string = "CONCAT_WS('".$infix_array[0]."'," . $concat_fields_array[0] . "," . $concat_fields_array[1] . ")";
$i = 0;
foreach($concat_fields_array as $concat_field){
    if($i >= 2){
         $concat_string = "CONCAT_WS('".$infix_array[$i-1]."'," . $concat_string . "," . $concat_field . ")";
    }
    $i++;
}

This builds a nested CONCAT_WS like:

 CONCAT_WS(', ',CONCAT_WS(' - ',CONCAT_WS(':',CONCAT_WS(' ',field1,field2),field3),field4),field5)

Would wrapping the fields in NULLIF like NULLIF(field1, ”) get rid of extra infixes?

Is there a faster function to accomplish this?

  • 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-05-29T08:55:20+00:00Added an answer on May 29, 2026 at 8:55 am

    You don’t have a problem with NULL fields, because CONCAT_WS will skip NULL values (together with separator). Regarding empty strings (which will not be skipped), you can just turn them into NULLs before doing this. If you cannot update the database itself, then make a query like:

    select <your expression> from (select case when col1 = '' then null else col1 end as col1 from my_table);
    

    so, just convert it to null on the fly and make your select from that inner query.

    In order to make this work, you will have to use nullif, so instead of:

    CONCAT_WS(a, CONCAT_WS(c, d))
    

    use

    NULLIF(CONCAT_WS(a, NULLIF(CONCAT_WS(c, d), '')), '')
    

    I don’t think that you will find a faster way of doing that. Another option might be to just select those fields and then make concatenation on the PHP side, but that is possible only if your resulting concatenation is not part of some WHERE clause further in your query.

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

Sidebar

Related Questions

I'm currently dynamically building an asp:table which contains checkboxes these checkboxes have a CheckChanged
I am building an app that needs to dynamically/programatically know of and use different
I'm dynamically building a nhibernate projected query that needs to implement paging. Something like...
I am working on a class for building drop down buttons dynamically. Here is
I'm currently building a Gridview that has expandable rows. Each row contains a dynamically
We are dynamically building some SQL statements and we are utilizing the IN operator.
Is there an easy way of dynamically building a filepath in .Net? At the
I'm trying to evaluate an XPath varable I'm building dynamically based on the position
I am building a grid dynamically and putting buttons in one of the columns.
I'm building a .NET application where I want to dynamically add an email address

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.