Home > Software design >  sql sorting a column with numbers and words
sql sorting a column with numbers and words

Time:05-17

I'm having issues sorting a column of numbers and words a specific Ascending order of type nvarchar.

when I run this query

select GradedDescriptions from cards order by GradedDescriptions asc

I get

10
5
8
8.5
Authentic
Authentic
Black 10

But my desired result is

Authentic
5
8
8.5
10
Black 10

Is there a way to put Authentic before the numbers and Black 10 at the end?

CodePudding user response:

Perhaps something like this

Declare @YourTable Table ([GradedDescriptions] varchar(50))
Insert Into @YourTable Values 
 ('10')
,('5')
,('8')
,('8.5')
,('Authentic')
,('Black 10')
 
Select * from @YourTable 
Order by try_convert(money,right([GradedDescriptions],3))
        ,[GradedDescriptions]

Results

GradedDescriptions
Authentic
5
8
8.5
10
Black 10
  • Related