CONCAT_WS()

SQL · String Functions

Theory

CONCAT_WS(separator, str1, str2, ...) joins strings and puts the separator between each pair, so you write the separator once instead of repeating it. CONCAT_WS(', ', 'Asha', 'IT', 'Jaipur') returns 'Asha, IT, Jaipur'. The name stands for 'concatenate with separator'.

Its most useful feature is how it treats NULLs: in MySQL, Postgres and SQL Server, NULL arguments are skipped entirely, together with their separator. So CONCAT_WS(', ', 'Meera', 'IT', NULL) gives 'Meera, IT' with no dangling comma. With plain || the same input would return NULL. Empty strings are not skipped, though, so '' still produces a double separator. In MySQL, a NULL separator makes the whole result NULL. Oracle and older SQLite builds do not have CONCAT_WS; there you emulate it with || and COALESCE(', ' || col, ''), as the diagram does.

Why we use it

Addresses, tags, CSV-style lines and full names often have optional parts. Building them with || means many COALESCE calls and stray separators. CONCAT_WS handles the separator and missing values in one call.

When to use it

Use CONCAT_WS when three or more values share one separator, especially if some can be NULL. Use CONCAT or || for two parts or mixed separators. For joining values from many rows, use STRING_AGG, GROUP_CONCAT or LISTAGG instead.

How it works

  1. Read the separator and the string arguments
  2. Is the separator NULL? Yes: MySQL returns NULL. No: Continue.
  3. Drop every NULL argument
  4. Join the remaining values with the separator between them
  5. Return the joined string (no dangling separator)

Syntax

CONCAT_WS(separator, str1, str2, ...)

Example

SELECT CONCAT_WS(', ', name, department) FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic