订购时省略字母

时间:2014-05-07 06:37:34

标签: sql database sql-server-2008-r2

我有以下查询:

 select U.IdUser,U.memberID,SL.Title from users U
                   inner join statuslist SL on
                   U.idStatus=SL.IdStatus
                   where U.idOrg='1'and isnull(U.memberID,'')!=''
                   order by   U.memberID

它给了我结果:

enter image description here

在这里,我希望B12会员ID在B110之前出现(意味着12来自110之前的原因)。

我怎样才能实现它?

我在下面试过......

select U.IdUser,U.memberID,SL.Title from users U
                   inner join statuslist SL on
                   U.idStatus=SL.IdStatus
                   where U.idOrg='1'and isnull(U.memberID,'')!=''
                   order by     Substring(U.memberID,2,LEN(U.memberID))

但没有给出正确的结果。

请帮帮我。

1 个答案:

答案 0 :(得分:1)

这是因为order by with stringsorder by with numbers

之间存在差异
select U.IdUser,U.memberID,SL.Title from users U
               inner join statuslist SL on
               U.idStatus=SL.IdStatus
               where U.idOrg='1'and isnull(U.memberID,'')!=''
               order by     CAST(Substring(U.memberID,2,LEN(U.memberID)-1) AS INT);

select U.IdUser,U.memberID,SL.Title from users U
               inner join statuslist SL on
               U.idStatus=SL.IdStatus
               where U.idOrg='1'and isnull(U.memberID,'')!=''
               order by     CONVERT(INT,Substring(U.memberID,2,LEN(U.memberID)-1));