Dynamic order direction

You could have two near-identical ORDER BY items, one ASC and one DESC, and extend your CASE statement to make one or other of them always equal a single value: ORDER BY CASE WHEN @OrderDirection = 0 THEN 1 ELSE CASE WHEN @OrderByColumn = ‘AddedDate’ THEN CONVERT(varchar(50), AddedDate) WHEN @OrderByColumn = ‘Visible’ THEN CONVERT(varchar(2), Visible) … Read more

“Order By” using a parameter for the column name

You should be able to do something like this: SELECT * FROM TableName WHERE (Forename LIKE ‘%’ + @SearchValue + ‘%’) OR (Surname LIKE ‘%’ + @SearchValue + ‘%’) OR (@SearchValue=”ALL”) ORDER BY CASE @OrderByColumn WHEN 1 THEN Forename WHEN 2 THEN Surname END; Assign 1 to @OrderByColumn to sort on Forename. Assign 2 to … Read more

Linq-to-sql orderby thenby

How can I do an orderby thenby using the above syntax without using extension syntax. Use a comma between the fields: orderby a, b And what does the orderby, orderby do? When you use orderby twice in a row the elements conceptually will first be sorted using the first orderby, and then sorted again using … Read more

mysql order varchar field as integer

I somehow didn’t manage to run the query with CAST. I was always getting Error Code: 1064 near “DECIMAL” (or other numeric type that I chose). So, I found another way to sort varchar as numbers: SELECT * FROM mytable ORDER BY ABS(mycol) A bit simpler and works in my case.

JPA/hibernate sorted collection @OrderBy vs @Sort

If you want to avoid non-standard annotations, you could make kittens use some sorted Collection implementation. This would ensure that kittens is always in sorted order. Something like this: @Entity public class Cat { @OneToMany(mappedBy = “cat”, cascade = CascadeType.ALL) @OrderBy(“name ASC”) private SortedSet<Kitten> kittens = new TreeSet<>(); } Note that this approach also requires … Read more