How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Handle MySQL CONCAT Returning NULL
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Handle MySQL CONCAT Returning NULL
Web Development

How to Handle MySQL CONCAT Returning NULL

how7o
By how7o
Last updated: May 22, 2026
4 Min Read
Fix MySQL CONCAT returning NULL with COALESCE or CONCAT_WS
SHARE

To handle MySQL’s CONCAT() returning NULL when any input is NULL, either wrap each potentially-NULL column with COALESCE(col, ''), or switch to CONCAT_WS(), which silently skips NULL arguments.

Contents
  • The problem
  • Fix 1 — wrap nullables in COALESCE()
  • Fix 2 — CONCAT_WS() skips NULL automatically
  • Better use of CONCAT_WS() — let it provide the spaces
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 on MySQL 8.0 and MariaDB 10.11. Originally published 2024-03-28, rewritten and updated 2026-05-17.

The problem

SELECT CONCAT(first_name, ' ', last_name, ' (', username, ')')
FROM users;

-- For a user with last_name = NULL, the whole expression is NULL.

MySQL’s CONCAT() follows the SQL standard: any NULL argument poisons the entire result. That’s right for arithmetic but not for assembling display names.

MySQL CONCAT NULL fix — COALESCE wrap, CONCAT_WS skip, || operator

Fix 1 — wrap nullables in COALESCE()

SELECT CONCAT(
    COALESCE(first_name, ''),
    ' ',
    COALESCE(last_name, ''),
    ' (',
    username,
    ')'
)
FROM users;

COALESCE(field, '') returns the field if it’s not NULL, otherwise an empty string. Wrap every column that could be NULL. Verbose, but explicit — you can see exactly which fields are nullable from the SQL.

Fix 2 — CONCAT_WS() skips NULL automatically

SELECT CONCAT_WS('', first_name, ' ', last_name, ' (', username, ')')
FROM users;

CONCAT_WS() (“CONCAT With Separator”) was built to skip NULL arguments. The first argument is the separator; the rest are the values to join. Using an empty separator '' makes it behave like CONCAT() for non-NULL inputs while silently dropping NULLs.

Better use of CONCAT_WS() — let it provide the spaces

-- Separator = single space, NULLs skipped, no leading/trailing space if NULL is at the edge
SELECT CONCAT_WS(' ', first_name, last_name) AS full_name
FROM users;

-- For users with NULL last_name -> just "John"
-- For users with NULL first_name -> just "Doe"
-- For both NULL                  -> "" (empty string, not NULL)

When the separator is a real character (space, comma, etc.), CONCAT_WS() shines: it doesn’t double-up the separator around NULL values, so you don’t get “John Doe” with a double space when one side is missing.

Frequently asked questions

Why does CONCAT() return NULL when any argument is NULL?

It’s the SQL standard’s three-valued logic — any operation involving NULL produces NULL, on the principle that NULL means “unknown” and combining anything with an unknown is itself unknown. CONCAT('a', NULL, 'b') isn’t ‘aNULLb’ or ‘ab’ by default; it’s NULL. CONCAT_WS() deliberately breaks this rule, because building display names from optional fields is the common case where NULL-as-skip is what you want.

What’s the difference between COALESCE() and IFNULL()?

IFNULL(a, b) takes exactly two arguments — return a if not NULL, else b. COALESCE(a, b, c, …) takes any number — returns the first non-NULL. COALESCE is SQL-standard and portable to PostgreSQL/SQL Server; IFNULL is MySQL/MariaDB-only. For a CONCAT replacement, COALESCE(field, '') reads cleanly.

Does CONCAT_WS with an empty separator behave exactly like CONCAT?

Almost — both produce the same string for non-NULL inputs. The difference is NULL handling: CONCAT_WS('', 'a', NULL, 'b') returns 'ab'; CONCAT('a', NULL, 'b') returns NULL. Performance is identical; pick by readability and NULL-tolerance needs.

Will this work in PostgreSQL too?

PostgreSQL has CONCAT() (NULL-tolerant — opposite default from MySQL) and CONCAT_WS() (also NULL-tolerant). Counter-intuitive when you switch databases. To get MySQL-style strict CONCAT in PostgreSQL, use the || operator, which does return NULL when any operand is NULL.

Related guides

  • How to Combine Multiple Columns as One String in MySQL
  • How to Get Only the Digits from a String in MySQL
  • How to Remove a Specific String from a Column in MySQL

References

MySQL CONCAT(): dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat. MySQL CONCAT_WS(): dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat-ws. MySQL COALESCE(): dev.mysql.com/doc/refman/8.0/en/comparison-operators.html#function_coalesce.

TAGGED:mysqlsqlstrings

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Get the selected radio button value with jQuery How to Get the Selected Radio Button Value in jQuery
Next Article Install HAProxy on AlmaLinux, Rocky Linux, or RHEL How to Install and Set Up HAProxy on AlmaLinux, Rocky, or RHEL
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Run Laravel queue workers with Supervisor
How to Run Laravel Queue Workers in Production with Supervisor
May 23, 2026
Nginx as a reverse proxy for a Node.js app on Ubuntu
How to Set Up Nginx as a Reverse Proxy for Node.js on Ubuntu
May 23, 2026
Install and configure Redis on Ubuntu for Laravel and WordPress
How to Install and Configure Redis on Ubuntu (for Laravel & WordPress)
May 23, 2026
Harden a fresh Ubuntu VPS with UFW, Fail2Ban, and SSH key auth
How to Harden a Fresh Ubuntu VPS: UFW + Fail2Ban + SSH Key Auth
May 23, 2026
Set up Let's Encrypt SSL with Certbot on Ubuntu
How to Set Up Let’s Encrypt SSL with Certbot on Ubuntu (Apache & Nginx)
May 23, 2026

You Might Also Like

CSS page break for printing shown in a print preview layout
Web Development

CSS Page Break for Printing: How to Split a Web Page Into Multiple Printed Pages

6 Min Read
WooCommerce homepage filter to hide out of stock products
Web Development

Hide Out of Stock Products from Homepage in WooCommerce (Keep Them Visible Elsewhere)

5 Min Read
Scroll to an element on a web page with jQuery
Web Development

How to Scroll to an Element Using jQuery

4 Min Read
Laravel unknown column CONCAT fix — DB::raw and selectRaw bypass identifier escaping
Web Development

How to Fix “Unknown column ‘CONCAT'” in Laravel

8 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?