通过asc Sql查询排序

时间:2015-01-08 11:14:24

标签: sql sql-server

我有asc查询的sql命令,我必须订购包含整数值的varchar字段(我不能将数据类型更改为int,因为该表中已有大量数据更改可能会丢失数据)。排序结果如下

1
2
3
.
.
8
9
10
101
102
103
.
.
109
11
110
111
.
.
119
12

结果应为

1
2
3
.
.
8
9
10
11
12

查询是

      dbAccess.execute("SELECT [id],[SNo],LetterSNO,[SubjectOfLetter],[FrmWhere],[WhomTosend],
(Cast(Day(DateOfRecieve) as Varchar(3))+'/'+Cast(Month(DateOfRecieve) as Varchar(2))+'/'+Cast(Year(DateOfRecieve) as Varchar(4))) as DateOfRecieve ,[Remarks] ,[User_IPAddress], 
(Cast(Day(DateOfSending) as Varchar(3))+'/'+Cast(Month(DateOfSending) as Varchar(2))+'/'+Cast(Year(DateOfSending) as Varchar(4))) as DateOfSending
 FROM [MpscdrcTESTING].[dbo].[tbl_PostalEntry] 
where DateOfRecieve between '" + TextBox1.Text.Trim() + "' and  '" + TextBox2.Text.Trim() + "' order by SNo asc";

请帮助

1 个答案:

答案 0 :(得分:1)

试试这个

ORDER BY CAST(sNO as INT)