SQL Server按数字varchar排序

时间:2014-10-03 20:19:10

标签: sql-server sql-order-by azure-sql-database

我有这个问题:

select  cast(MONTH(c.dataregistro) as VARCHAR(2)) as DATA, numberofquotesreturned, count(*) AS CountNumberQuotes from contratacao c
inner join teleportpedidocotacao tpc  on c .idcontratacao = tpc.idcontratacao
where cast(c.dataregistro as date) > '2014-01-01 00:00:00' AND numberofquotesreturned = 5
group by numberofquotesreturned, cast(MONTH(c.dataregistro) as VARCHAR(2)) 
order by cast(MONTH(c.dataregistro) as VARCHAR(2))

结果是:

10  5   347
2   5   112
3   5   914
4   5   786
5   5   1184
6   5   2361
7   5   2430
8   5   2184
9   5   2709

我想按第一栏的整数来订购,你可以看到' 10'先到了。我该如何解决?

1 个答案:

答案 0 :(得分:1)

只需将广告素材移至varchar

即可
select  MONTH(c.dataregistro) as DATA, numberofquotesreturned, count(*) AS CountNumberQuotes from contratacao c
inner join teleportpedidocotacao tpc  on c .idcontratacao = tpc.idcontratacao
where cast(c.dataregistro as date) > '2014-01-01 00:00:00' AND numberofquotesreturned = 5
group by numberofquotesreturned, MONTH(c.dataregistro)
order by MONTH(c.dataregistro)