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
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026
Tailscale mesh — peer-to-peer connections between devices, coordination server
How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
May 24, 2026

You Might Also Like

Remove all non-numeric characters from a PHP string
Web Development

How to Remove All Non-Numeric Characters from a String in PHP

4 Min Read
Display only the current date in Laravel using Carbon
Web Development

How to Display Only the Current Date in Laravel (Carbon Examples)

4 Min Read
JavaScript check Unicode character — regex, codePointAt, Unicode property escape
Web Development

How to Check if a JavaScript String Contains a Unicode Character

38 Min Read
Laravel foreign key constraint linking posts.user_id to users.id in a schema diagram
Web Development

How to Add Foreign Keys in Laravel Migration

6 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?