Sort string as number in sql server

前端 未结 9 1491
不知归路
不知归路 2020-12-20 16:41

I have a column that contains data like this. dashes indicate multi copies of the same invoice and these have to be sorted in ascending order

790711
790109-1         


        
9条回答
  •  梦毁少年i
    2020-12-20 17:33

    Order by each part separately is the simplest and reliable way to go, why look for other approaches? Take a look at this simple query.

    select *
    from Invoice
    order by Convert(int, SUBSTRING(invoiceid, 0, CHARINDEX('-',invoiceid+'-'))) asc,
             Convert(int, SUBSTRING(invoiceid, CHARINDEX('-',invoiceid)+1, LEN(invoiceid)-CHARINDEX('-',invoiceid))) asc
    

提交回复
热议问题