Click here to Skip to main content
15,349,004 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
declare @temp Varchar(100) = '122,122,456,1333,55555,XYZ,ABC,XY1'

How do I sort the Values in SQL? (Values in ascending order, and NOT order in split is done)

How do I remove the duplicate values in SQL?

need 2 separate queries for each use case

What I have tried:

-----------------------CLUELESS----------------------------
Posted
Updated 9-Sep-20 0:25am
v3

STRING_SPLIT (Transact-SQL) - SQL Server | Microsoft Docs[^]
Available in SQL Server 2016 and later.
SQL
-- Sort the values:
SELECT value FROM STRING_SPLIT(@temp, ',') ORDER BY value;

-- Remove duplicates:
SELECT DISTINCT value FROM STRING_SPLIT(@temp, ',');
If you're using an earlier version of SQL Server, Google will find you an alternative string splitting function.
   
Comments
Vishal0903 9-Sep-20 5:29am
   
I am using SQL 2012
Richard Deeming 9-Sep-20 5:30am
   
If you're using an earlier version of SQL Server, Google will find you an alternative string splitting function.
In response to OP comment here's a link from a question I answered yesterday Best split function - SQL Server Forums[^]
   

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