Learn SQL/Intermediate/String Functions/SQL Combine Text Columns

SQL Combine Text Columns

Assembling a single display value from multiple source columns — CONCAT_WS, applied to real campaign data.

What & Why

A common reporting need: combine several separate columns into one readable string. This reuses the CONCAT_WS pattern from earlier in this series, applied to a genuinely useful combination.

See How It Works

BUSINESS QUESTION

Marketing wants a campaign label containing its name and channel.

idnamechannelspendstart_dateend_datestatustarget_segment
1Spring Launchgoogle_ads55000.002024-01-152024-03-31activesmb
2Retention Webinaremail45000.002024-02-102024-04-15activeenterprise
3Finance Retargetinglinkedin50000.002024-03-122024-05-31activeenterprise
4Enterprise Searchgoogle_ads60000.002024-04-012024-06-30activeenterprise
EXAMPLE QUERY
SELECT
  id AS campaign_id,
  CONCAT_WS(' · ', name, channel) AS campaign_label
FROM marketing.campaigns
ORDER BY campaign_id;
RESULT — exact output from the displayed Queryflo rows
campaign_idcampaign_label
1Spring Launch · google_ads
2Retention Webinar · email
3Finance Retargeting · linkedin
4Enterprise Search · google_ads

The labels concatenate the displayed campaign name and channel.

Now You Try

Practice this concept

Marketing wants a campaign label containing its name and channel.

Available schema
marketing

Prefix tables with marketing.table_name.

campaign_idcampaign_label
marketing.campaigns
ColumnType
idinteger
nametext
channeltext
spendnumeric
start_datedate
end_datedate
statustext
target_segmenttext
legacy_idtext
query.sql
Intermediate business practice

Sign up free to try it on a real business scenario