Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have dynamic string like
SQL
DECLARE @string NVARCHAR(MAX)='Dear {CustomerName},Your invoice ({InvoiceNumber}) has been created. An overview of the invoice is available below:Invoice: {InvoiceNumber}Invoice Date: {InvoiceDate}Amount: {GrandTotal}Please go through it and confirm the invoice. We look forward to working with you again.Regards,Accounts Team{FirmName}'


i need all value between {}
eg
CustomerName,InvoiceNumber,InvoiceDate,GrandTotal

What I have tried:

SQL
;WITH yourTable AS (
    SELECT 'Dear {CustomerName},Your invoice ({InvoiceNumber}) has been created. An overview of the invoice is available below:Invoice: {InvoiceNumber}Invoice Date: {InvoiceDate}Amount: {GrandTotal}Please go through it and confirm the invoice. We look forward to working with you again.Regards,Accounts Team{FirmName}' AS col
)

SELECT
    SUBSTRING(col,
              CHARINDEX('{', col) + 1,
              CHARINDEX('}', col) - CHARINDEX('{', col) - 1) AS output
FROM yourTable;
Posted
Updated 10-Jan-22 23:05pm

Something like this should work:
SQL
DECLARE @string NVARCHAR(MAX) = N'Dear {CustomerName},Your invoice ({InvoiceNumber}) has been created. An overview of the invoice is available below:Invoice: {InvoiceNumber}Invoice Date: {InvoiceDate}Amount: {GrandTotal}Please go through it and confirm the invoice. We look forward to working with you again.Regards,Accounts Team{FirmName}'

WITH cteTally As
(
    -- TODO: Use a tally table here if you have one.
    SELECT TOP (LEN(@string))
        ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) As RN
    FROM
        sys.all_columns
),
cteIndices As
(
    SELECT
        S.RN As StartIndex,
        CHARINDEX('}', @string, S.RN + 1) As EndIndex
    FROM
        cteTally As S
    WHERE
        SUBSTRING(@string, S.RN, 1) = '{'
)
SELECT DISTINCT
    SUBSTRING(@string, StartIndex + 1, EndIndex - StartIndex - 1)
FROM
    cteIndices
WHERE
    EndIndex > StartIndex + 1
;
Output:
CustomerName
FirmName
GrandTotal
InvoiceDate
InvoiceNumber
 
Share this answer
 
Comments
Maciej Los 11-Jan-22 4:46am    
Good one! :)
Using table and one CTE:

SQL
CREATE TABLE TempData
(
  LongText VARCHAR(MAX)
);

INSERT INTO TempData (LongText)
VALUES ('Dear {CustomerName},Your invoice ({InvoiceNumber}) has been created. An overview of the invoice is available below:Invoice: {InvoiceNumber}Invoice Date: {InvoiceDate}Amount: {GrandTotal}Please go through it and confirm the invoice. We look forward to working with you again.Regards,Accounts Team{FirmName}')

;WITH CTE AS
(
  SELECT 1 AS RN, LongText, CHARINDEX('{', LongText)+1 AS StartPos, CHARINDEX('}', LongText)-1 AS EndPos
  FROM TempData
  WHERE CHARINDEX('{', LongText)>0 AND CHARINDEX('}', LongText)>0
  UNION ALL
  SELECT RN + 1 AS RN, LongText, CHARINDEX('{', LongText, StartPos)+1 AS StartPos, CHARINDEX('}', LongText, EndPos+2)-1 AS EndPos
  FROM CTE
  WHERE CHARINDEX('{', LongText, StartPos)>0 AND CHARINDEX('}', LongText, EndPos+2)>0
)
SELECT RN, SUBSTRING(LongText, StartPos, EndPos - StartPos+1) AS TextBetweenParenthesis
FROM CTE;


Result:
RN 	TextBetweenParenthesis
1 	CustomerName
2 	InvoiceNumber
3 	InvoiceNumber
4 	InvoiceDate
5 	GrandTotal
6 	FirmName


SQL Server 2019 | db<>fiddle[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900