Table Transformer - Substring based on Expression
Asked by MOh G:

I am trying to create columns out of a single text multiline column using SQL

Data

-----

Priority \| Medium  
Project \| Proj1  
Category \| Cat1

Desired Result

Three columns: Priority, Project, Category

I know how to write a case statement easily

select

Case when Desc \[condition\] then end 'Priority',

Case when Desc \[condition\] then end 'Project',

..etc

Now to extract values after \| , I could write the a Java script or T-SQL that uses a substring and a charindex to locate text between \| and newline.

Any help with transformer SQL? It should be easy but my luck is not hitting for now


Answers:
author: Katerina Rudkovskaya [Stiltsoft]
body: Hello [@MOh G](/t5/user/viewprofilepage/user-id/4892582)  

Could you please confirm if your use case is like this (see the screenshot below)?  

![2022-04-30_00h09_25.png](https://community.atlassian.com/t5/image/serverpage/image-id/195344iF07CD4FEE7705B08/image-size/large?v=v2&px=999 "2022-04-30_00h09_25.png")  

Thanks!  

Katerina

comments: author: MOh G
body: Hi Katrina

Yes, this is the desired result


author: MOh G
body: [@Katerina Rudkovskaya \[Stiltsoft\]](/t5/user/viewprofilepage/user-id/3738229) I used Split to separate lines then another split to use \| symbol. Somehow the newline split is not working


author: Katerina Rudkovskaya [Stiltsoft]
body: Hi [@MOh G](/t5/user/viewprofilepage/user-id/4892582),  

You can try to use the**SUBSTRING** funtion. Please see the example below:  

![2022-05-07_00h03_29.png](https://community.atlassian.com/t5/image/serverpage/image-id/196336iADAC6742DF183772/image-size/large?v=v2&px=999 "2022-05-07_00h03_29.png")  

SELECT  
SUBSTRING(T1.'column 1',11,7) AS 'Priority',  
SUBSTRING(T1.'column 1',28,5) AS 'Project',  
SUBSTRING(T1.'column 1',44,4) AS 'Category'  
FROM T\*  

![2022-05-07_00h03_33.png](https://community.atlassian.com/t5/image/serverpage/image-id/196335iE0217C8E5CC2E576/image-size/large?v=v2&px=999 "2022-05-07_00h03_33.png")![2022-05-07_00h03_36.png](https://community.atlassian.com/t5/image/serverpage/image-id/196337i8AC7C91E164A5A69/image-size/large?v=v2&px=999 "2022-05-07_00h03_36.png")  

BR,  
Katerina


author: MOh G
body: [@Katerina Rudkovskaya \[Stiltsoft\]](/t5/user/viewprofilepage/user-id/3738229) the source string is rarely, if ever, fixed. a regular expression is needed here


author: Katerina Rudkovskaya [Stiltsoft]
body: Hi [@MOh G](/t5/user/viewprofilepage/user-id/4892582),  

Then you need more complex SQL query like this (please see the example below):  

SELECT  
TT1.'column 2'-\>match("\[$\]P:(.+?)\[$\]")-\>\[1\] AS 'Priority',  
TT1.'column 2'-\>match("\[$\]Pr:(.+?)\[$\]")-\>\[1\] AS 'Project',  
TT1.'column 2'-\>match("\[$\]C:(.+?)\[$\]")-\>\[1\] AS 'Category'  
FROM (SELECT  
T1.'column 1'-\>replaceAll("Priority \| ", "$P:")  
-\>replaceAll("Project \| ", "$Pr:")  
-\>replaceAll("Category \| ", "$C:")  
-\>replaceAll("Another \| ", "$")  
+ "$"  
AS 'column 2'  
FROM T1) AS TT1  

![2022-05-10_14h24_52.png](https://community.atlassian.com/t5/image/serverpage/image-id/196640iD594313C797BA141/image-size/large?v=v2&px=999 "2022-05-10_14h24_52.png")![2022-05-10_14h25_14.png](https://community.atlassian.com/t5/image/serverpage/image-id/196639iB8D840A13BD629EA/image-size/large?v=v2&px=999 "2022-05-10_14h25_14.png")![2022-05-10_14h25_16.png](https://community.atlassian.com/t5/image/serverpage/image-id/196638iF5C9B2C46F4DD1C6/image-size/large?v=v2&px=999 "2022-05-10_14h25_16.png")  

Katerina


Tags: confluence-cloud

server

table-transformer