sql server - Reorder rows in a query TSQL -
sql server - Reorder rows in a query TSQL -
i've requirement sort rows returned table based on condition.
for example, simple select on table produces next result. see displaydaynumber starts 1, , continues till 7.
i want sort info row dayofweekname = tuesday appears first row, , rest of rows order after it.
any help reorder these rows highly appreciated.
you can utilize case
:
... order case when dayofwekname = 'tuesday' 0 else 1 end asc, displaydaynumber asc
update want tuesday
first, rest of rows should ordered "naturally", next rows wednesday
, thirsday
, friday
etc. until monday
?
you utilize query:
declare @weekstart varchar(10) = 'tuesday' select d.* dates d order case when dayofweekname = @weekstart 0 else 1 end asc, case when displaydaynumber < (select displaydaynumber dates dayofweekname = @weekstart) 7 + displaydaynumber else displaydaynumber end asc
demo
sql-server tsql
Comments
Post a Comment