SQL Server date column sorting dates automatically -
i have table date column. inserting dates in column using datetimepicker
in c#.
everything works fine. want dates inserted in table ordered. when add today's date in 1 row , add yesterday's date or past date, gets added in 2nd row ofc.
i want added in 1st row instead. before today's date, make dates in order.
is there property of date column can use ensure dates order?
there absolutely no implicit order in sql server datatables! no index, no clustered index, no trick... simple select * table
may come sorted data - or not...
if need data sorted in special way must add order by
now 1 solution problem:
create updateable view (https://msdn.microsoft.com/en-us/library/ms180800.aspx) on table ordered want it. commuicate through view...
important is: when specify select top 100 percent * yourtable order yourcolumn
view not come sorted data...
the trick here introduce sorting row_number()
i took testing 1 of tables. make more difficult took 1 identity column.
and please keep in mind: sure order outermost order by
create view vwonetablewithidentityid select row_number() over(order sortcolumn) orderedinx ,* onetablewithidentityid go select * vwonetablewithidentityid; go set identity_insert onetablewithidentityid on; insert vwonetablewithidentityid(onetablewithidentityidid,anotherid,sortcolumn) values(-5,1081,'aabc'); set identity_insert onetablewithidentityid off; go select * vwonetablewithidentityid go delete vwonetablewithidentityid onetablewithidentityidid=-5; go select * vwonetablewithidentityid; go drop view vwonetablewithidentityid;
Comments
Post a Comment