Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordering a string containing an integer inside SQL

I have the below content inside a table in SQL

enter image description here

I tried to order by title but I didn't work, is there any SQL statement to use so I can order by the number inside InterSciences Competition?

like image 399
Sora Avatar asked Nov 18 '25 09:11

Sora


1 Answers

You can try like following query.

;WITH cte 
     AS (SELECT title, 
                Cast(Substring(title, Charindex('(', title) + 1, 
                     Charindex(')', title) - 
                     Charindex('(', title) - 1) 
                     AS INT) AS OC 
         FROM   yourtable) 
SELECT * 
FROM   cte 
ORDER  BY oc 

In above query, the number between brackets is extracted and converted to INT for ordering.

Online Demo

like image 130
PSK Avatar answered Nov 20 '25 07:11

PSK