I have the below data in a table. Now I need to separate the string based on the "-" value. The separation needs to be done based on the below output scenarios.
| Name | Required Output |
|---|---|
| itim-connect-prd | CONNECT |
| WHATS-PRD | WHATS |
| Commitment-FinOps | COMMITMENT |
| itim-xbl-hml | XBL |
| itim-audit-hml | AUDIT |
| BSC-AWS-Accounts-Roots | AWS |
For example, in the above table, the [Name] column is the source data and I need to convert the [Name] column to the [Required Output] column.
To get the above required output column, I have used the below SQL Script... But it didn't work for the scenarios like Commitment-FinOps&WHATS-PRD values.
SQL script used
UPPER(SUBSTRING(SUBSTRING([Name],CHARINDEX('-',[Name])+1,CHARINDEX('-', [Name], CHARINDEX('-', [Name]) + 1)),1,CHARINDEX('-',SUBSTRING([Name],CHARINDEX('-',[Name])+1,CHARINDEX('-', [Name], CHARINDEX('-', [Name]) + 1)))-1)) AS Required Output
Can anyone modify the script so it will work for all scenarios.
Apparently you want the first substring if there are two parts and second one if there are more. You can use a bunch of
charindex:In SQL Server 2022 you can use
string_split.