简体   繁体   中英

SQL: Extract a string from between the 2nd and 3rd dashes (-), if they exist. Else N/A

How to write SQL to extract the string from the 2nd and 3rd dashes on the "Campaign" column to a new column "Language".

If no 2nd and 3rd dashes, then "N/A". Thank you for your help!

Campaign Language
na--en-abc en
na-branded N/A
ap--jp-xyz jp
eu-uk-en-jjj en

Consider below simple approach

select Campaign,
  ifnull(split(Campaign, '-')[safe_offset(2)], 'N/A') as Language
from your_table

If applied to sample data in your question - output is

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM