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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:13:06+00:00 2026-05-20T10:13:06+00:00

I have a query that is selecting about 20 columns, all are varchars. Some

  • 0

I have a query that is selecting about 20 columns, all are varchars.

Some will be empty, which is just fine. But I want to put a default string of “——-” for any columns that are empty.

Here’s what I have so far, that works:

$sql = "SELECT col1, ";
$sql .= "IF(`col2` IS NULL,'----------',`col2`) AS `col2`, ";
$sql .= "IF(`col3` IS NULL,'----------',`col3`) AS `col3`, ";
$sql .= "IF(`col4` IS NULL,'----------',`col4`) AS `col4`, ";
$sql .= "IF(`col5` IS NULL,'----------',`col5`) AS `col5`, ";
$sql .= "IF(`col6` IS NULL,'----------',`col6`) AS `col6`, ";
.......and so on.....
$sql .= "FROM `tableName`";

This works for me, but I am wondering if there’s a more “global” way, instead of specifying each column.

  • 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-20T10:13:07+00:00Added an answer on May 20, 2026 at 10:13 am

    Following mfloryan advice you can easily generate your string with a loop and implode() function

    <?php
    $sql = 'select col1,';
    $a = array();
    for ($i=2;$i<=20;$i++){
    $a[] = "coalesce(col".$i.",'-----') as col".$i."\r\n";
    }
    $sql.=implode(',',$a);
    $sql.=" from table_name";
    echo $sql;
    ?>
    

    edit. Note that information_schema is a wonderful tool to create dynamic SQL. This is an example

    mysql> use test;
    Database changed
    mysql> create table dynSQL(
        -> col1 varchar(10),
        -> col2 varchar(10),
        -> col3 varchar(10),
        -> col4 varchar(10)
        -> ) engine = myisam;
    Query OK, 0 rows affected (0.04 sec)
    
    mysql>
    mysql> insert into dynSQL(col1,col2,col3,col4)
        -> values (1,3,null,4),(10,null,4,null),(20,4,5,null);
    Query OK, 3 rows affected (0.00 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    mysql>
    mysql> set @str = (select concat('select col1,',group_concat(concat("coalesce(",column_name,",'----') as ",column_name,"\n")),' from dynSQL')
        -> from information_schema.columns
        -> where table_schema = 'test' and table_name = 'dynSQL' and column_name like 'col%' and column_name <> 'col1');
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> prepare stmt from @str;
    Query OK, 0 rows affected (0.00 sec)
    Statement prepared
    
    mysql> execute stmt;
    +------+------+------+------+
    | col1 | col2 | col3 | col4 |
    +------+------+------+------+
    | 1    | 3    | ---- | 4    |
    | 10   | ---- | 4    | ---- |
    | 20   | 4    | 5    | ---- |
    +------+------+------+------+
    3 rows in set (0.00 sec)
    
    mysql> deallocate prepare stmt;
    Query OK, 0 rows affected (0.00 sec)
    

    Regards. Nick

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

Sidebar

Related Questions

I have a Hive query which is selecting about 30 columns and around 400,000
I have a query that returns me around 6 million rows, which is too
I have a query that contains about 10 joins. Using a List View and
I have a query that is simply selecting from 3 tables. select from the
I am using in C# MYsql .I have query that works if I run
I have a query that successfully grabs the unique products from my products table
I have a query that basically combines tables of actions and selects from them
I have have this query that i want to execute. SELECT warehouse.expiry_date, pharmacy.expiry_date, drugs.active_substance,
I have a query that creates a result set like this: Rank Name 1
I have this query that returns the results by ordering it by focus.name ASC.

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.