how to reverse comma seprated values.

Rajesh Kumar Yadav 1 Reputation point
2023-11-10T10:25:24.18+00:00

hi,

i have coma separated vales in a table. what i want to reverse it like 'CLP,CAD,GBP' pls tell me how it can be done with out using xml in efficent way.

DECLARE @TBL AS TABLE
(COLUMN1  NVARCHAR(100))

INSERT INTO @TBL 
SELECT 'AUD,BRL,GBP,CAD,CLP'

SELECT COLUMN1 FROM @TBL
Azure SQL Database
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,872 questions
SQL Server Reporting Services
SQL Server Reporting Services
A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.
2,820 questions
SQL Server Analysis Services
SQL Server Analysis Services
A Microsoft online analytical data engine used in decision support and business analytics, providing the analytical data for business reports and client applications such as Power BI, Excel, Reporting Services reports, and other data visualization tools.
1,249 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,562 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Viorel 112.9K Reputation points
    2023-11-10T12:34:38.4066667+00:00

    Check an efficient method without XML:

    select *, (select string_agg(value, ',') within group (order by ordinal desc) from string_split(COLUMN1, ',', 1)) as RESULT
    from @TBL
    
    2 people found this answer helpful.
    0 comments No comments

  2. Javier Villegas 895 Reputation points MVP
    2023-11-10T13:50:31.17+00:00

    Hello, you can use below code

    SELECT   value FROM @TBL     CROSS APPLY STRING_SPLIT(COLUMN1, ',');
    
    0 comments No comments

  3. Javier Villegas 895 Reputation points MVP
    2023-11-10T13:51:41.46+00:00

    Hello,

    You can use below code

    SELECT   value FROM @TBL     CROSS APPLY STRING_SPLIT(COLUMN1, ',');
    
    0 comments No comments

  4. CosmogHong-MSFT 23,561 Reputation points Microsoft Vendor
    2023-11-13T02:23:07.63+00:00

    Hi @Rajesh Kumar Yadav

    Currently the enable_ordinal in STRING_SPLIT function is only available in Azure SQL Database, Azure SQL Managed Instance, Azure Synapse Analytics (serverless SQL pool only), and SQL Server 2022 (16.x) and later versions.

    If SQL Server 2022 (16.x) and later, please refer to Viorel's answer above.

    If you are using version between SQL Server 2017 (14.x) and SQL Server 2022 (16.x), then try this query:

    SELECT STRING_AGG(value, ',') WITHIN GROUP (ORDER BY [key] DESC) AS New_Column1
    FROM @TBL CROSS APPLY OPENJSON(N'["' + REPLACE(COLUMN1, ',', N'","') + N'"]') AS x
    

    If your version is before SQL Server 2017 (14.x), then you need to consider xml way.

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments