日期:2014-05-17 浏览次数:20636 次
select CardCode from (select CardCode,replace(tbProductData.CardCode,'W','999999') as tmp from tbProductData ) as a order by cast(tmp as int) --也是如果只有前面第一位字符是'W'的情況下
------解决方案--------------------
create table tbProductData ( CardCode varchar(50) ) insert into tbProductData values('1') insert into tbProductData values('10') insert into tbProductData values('11') insert into tbProductData values('12') insert into tbProductData values('2') insert into tbProductData values('20') insert into tbProductData values('22') insert into tbProductData values('3') insert into tbProductData values('30') insert into tbProductData values('w2') insert into tbProductData values('w3') select CardCode from (select CardCode,replace(tbProductData.CardCode,'W','9999999') as tmp from tbProductData ) as a order by cast(tmp as int) CardCode -------------------------------------------------- 1 2 3 10 11 12 20 22 30 w2 w3 (11 行受影响)
------解决方案--------------------
借用6楼数据:
create table tbProductData ( CardCode varchar(50) ) insert into tbProductData values('1') insert into tbProductData values('10') insert into tbProductData values('11') insert into tbProductData values('12') insert into tbProductData values('2') insert into tbProductData values('20') insert into tbProductData values('22') insert into tbProductData values('3') insert into tbProductData values('30') insert into tbProductData values('w2') insert into tbProductData values('w3') go select * from tbProductData order by (case when charindex('w',CardCode)>0 then REPLACE(cardcode,'w','')+999999 else CONVERT(int,cardcode) end) /* CardCode -------------------------------------------------- 1 2 3 10 11 12 20 22 30 w2 w3 (11 行受影响) */ go drop table tbproductdata