34,590
社区成员
发帖
与我相关
我的任务
分享
declare @table table (id int,bookCode int)
insert into @table
select 106,null union all
select 107,201001001 union all
select 108,null union all
select 109,201001002 union all
select 110,201001003 union all
select 111,201002001
select * from @table order by isnull(bookCode,999999999),id desc
/*
id bookCode
----------- -----------
107 201001001
109 201001002
110 201001003
111 201002001
108 NULL
106 NULL
*/
create table tb (id int,bookcode varchar(10))
insert into tb
select 106,null union all
select 108,null union all
select 107,'201001001' union all
select 109,'201001002' union all
select 110,'201001003' union all
select 111,'201002001'
drop table tb
select * from tb order by (case when bookcode is null then 1 else 0 end),id
/*
id bookcode
----------- ----------
107 201001001
109 201001002
110 201001003
111 201002001
106 NULL
108 NULL
--> 测试数据:#
if object_id('tempdb.dbo.#') is not null drop table #
create table #(id int, bookCode int)
insert into #
select 106, null union all
select 107, 201001001 union all
select 108, null union all
select 109, 201001002 union all
select 110, 201001003 union all
select 111, 201002001
select * from # order by isnull(bookCode,'999999999'), id desc
/*
id bookCode
----------- -----------
107 201001001
109 201001002
110 201001003
111 201002001
108 NULL
106 NULL
*/
select * from tb order by bookCode ,id desc
order by case when bookCode is null then 1 else 0 end,bookCode ,id desc
select * from tb order by bookCode desc,id
select id, isnull(bookCode,' ') as bookCode
from @table
order by replace(isnull(bookCode,''),'',999999999),id desc
declare @table table (id int,bookCode varchar(12))
insert into @table
select 106,null union all
select 107,'201001001' union all
select 108,null union all
select 109,'201001002' union all
select 110,'201001003' union all
select 111,'201002001' union all
select 112,' '
select id, isnull(bookCode,' ') as bookCode
from @table
order by isnull(replace(bookCode,' ',999999999),999999999),id desc
/*
id bookCode
----------- ------------
107 201001001
109 201001002
110 201001003
111 201002001
112
108
106
*/