sql server - How does work SQL with order by 0? -
sql server - How does work SQL with order by 0? -
how work?
select * table1 order case when field1 not null 0 end, id desc
i can replace 0 other integer values still right result.
as jurergen d pointed out, reduces possibilities in order by
2 candidates: null
, n
. whether n 0, 6, 562, or 391842, in status 1 (null
) show before in status 2 (n).
if, "correct result", mean "things null
in field1 show first", number, positive or negative, work n.
some sample data: can helpful see order by
clause doing, i'm going duplicate case
statement in select
.
declare @table1 table (id int identity(1,1), field1 int, fieldsomethingelse varchar(10)) insert @table1 (field1, fieldsomethingelse) values (1, 'some') insert @table1 (field1, fieldsomethingelse) values (2, 'thing') insert @table1 (field1, fieldsomethingelse) values (765, 'or') insert @table1 (field1, fieldsomethingelse) values (null, 'another') insert @table1 (field1, fieldsomethingelse) values (null, 'thing') select *, case when field1 not null 'itdoesn''t matter set here, same value every non-null row' end thisiswhatyouaresortingby @table1 order case when field1 not null 'itdoesn''t matter set here, same value every non-null row' end, id desc
the purpose, have figured out, allow order by
utilize id
field sort, separate out rows null
field1 value come first.
sql-server sql-order-by case
Comments
Post a Comment