Parsing text with common delimiters
So I've now had a chance to reuse this solution about three times and that tells me that it is common enough that I should share it. The principle here is that you have a piece of text with a good naming convention that is delimited with a common character. For example, "Name_Date_Product_Channel" which is seperated by underscores.
This piece of SQL code I'm including below will allow you to parse through the text and pull individual elements out as columns you name. You'll edit the elements in orange, green, and blue below.
Select `your_column_here`,
-- Parse Column 1 through 4.
Case WHEN (LENGTH(`your_column_here`) - LENGTH(REPLACE(`your_column_here`, '_', ''))) BETWEEN 1 AND 4
THEN REPLACE(LOWER(SUBSTRING_INDEX(`your_column_here`,'_',1)),' ','')
ELSE NULL
END as 'Name',
CASE
WHEN (LENGTH(`your_column_here`) - LENGTH(REPLACE(`your_column_here`, '_', ''))) BETWEEN 1 AND 4
THEN REPLACE(LOWER(SUBSTRING_INDEX(SUBSTRING_INDEX(`your_column_here`,'_',2),'_',-1)),' ','')
ELSE NULL
END as 'Date',
CASE
WHEN (LENGTH(`your_column_here`) - LENGTH(REPLACE(`your_column_here`, '_', ''))) BETWEEN 2 AND 4
THEN REPLACE(LOWER(SUBSTRING_INDEX(SUBSTRING_INDEX(`your_column_here`,'_',3),'_',-1)),' ','')
ELSE NULL
END as 'Product',
CASE
WHEN (LENGTH(`your_column_here`) - LENGTH(REPLACE(`your_column_here`, '_', ''))) BETWEEN 3 AND 4
THEN REPLACE(LOWER(SUBSTRING_INDEX(SUBSTRING_INDEX(`your_column_here`,'_',4),'_',-1)),' ','')
ELSE NULL
END as 'Channel'
FROM your_table_here
The case statments above can be extended to as many parsings as you need. If no match is found (in otherwords a null occurs in your data) it will just have a blank field.
I hope you find this as helpful as I have found it recently. If you use it and like it please upvote this entry!
Categories
- All Categories
- 1.7K Product Ideas
- 1.7K Ideas Exchange
- 1.5K Connect
- 1.2K Connectors
- 292 Workbench
- 4 Cloud Amplifier
- 8 Federated
- 2.8K Transform
- 95 SQL DataFlows
- 602 Datasets
- 2.1K Magic ETL
- 3.7K Visualize
- 2.4K Charting
- 693 Beast Mode
- 43 App Studio
- 39 Variables
- 658 Automate
- 170 Apps
- 441 APIs & Domo Developer
- 42 Workflows
- 5 DomoAI
- 32 Predict
- 12 Jupyter Workspaces
- 20 R & Python Tiles
- 386 Distribute
- 111 Domo Everywhere
- 269 Scheduled Reports
- 6 Software Integrations
- 113 Manage
- 110 Governance & Security
- 8 Domo University
- 30 Product Releases
- Community Forums
- 39 Getting Started
- 29 Community Member Introductions
- 98 Community Announcements
- Domo Community Gallery
- 4.8K Archive