.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

How can I use ORDER BY by passing different columns and sort orders?

Posted By:      Posted Date: October 27, 2010    Points: 0   Category :Sql Server

Hi all

Here is what I am trying to do:

declare @dateBegin as datetime, @dateEnd as datetime
declare @column as varchar(30), @order as varchar(4)
set @dateBegin = null
set @dateEnd  = null
set @column  = 'created'
set @order   = 'DESC'

	(@dateBegin is null or startTime >= @dateBegin) and (@dateEnd is null or endTime <= @dateEnd)

View Complete Post

More Related Resource Links

What is the meaning of Ascending\descending sort order of a column within an index

When I add a new index I can also specify the "sort order" as ascending or descending. What does it mean? does it affect performance? does it mean that the binary search tree will be set in a certain way?

Dynamic Data Entity Framework: Initial Sort Order for Views

Hello Steve,I'm trying to set an initial sort order for my View (not table) in Dynamic Data using Entity Framework 2. I have updated my Model and Imported the View. Now I'm trying to use the DisplayColumn attibutebut it's not working.[DisplayColumn("Product", "ParsingOrder", true)]Fabio

Sql Order by not working with decimal/money type columns

Error Message:- Cannot convert a char value to money. The char value has incorrect syntax. DECLARE @Order NVARCHAR(50) = 'asc', @OrderBy NVARCHAR(50) = 'Shape'SELECTtop 1 Row_Number() OVER(ORDER BY  CASE WHEN @Order IS NULL OR @Order = 'asc' THEN    CASE WHEN @OrderBy IS null THEN Shape    WHEN @OrderBy = 'Shape' THEN Shape    WHEN @OrderBy = 'Cut' THEN Cut    WHEN @OrderBy = 'Color' THEN Color    WHEN @OrderBy = 'Clarity' THEN Clarity    WHEN @OrderBy = 'Price' THEN FinalPrice    --MONEY    WHEN @OrderBy = 'Carat' THEN [Weight]   --DECIMAL   END  END ASC,  CASE WHEN @Order = 'desc' THEN    CASE WHEN @OrderBy IS null THEN Shape    WHEN @OrderBy = 'Shape' THEN Shape    WHEN @OrderBy = 'Cut' THEN Cut    WHEN @OrderBy = 'Color' THEN Color    WHEN @OrderBy = 'Clarity' THEN Clarity    WHEN @OrderBy = 'Price' THEN FinalPrice  --MONEY   WHEN @OrderBy = 'Carat' THEN [Weight]  --DECIMAL   END  

Dundas Map - Legend sort order

ello, We are using Dundas Maps together with Reporting Services 2008. Now we have come across a problem which we are not able to solve ourselves.   We produce a map showing each countries “ReportingFormat”. The SQL in the background  has the “order by ReportFormat” clause.   The values available in “ReportFormat” are shown in the legend. So far everything looks fine and the values within the legend are ordered correctly (E2B, Intdis, Not specified). See the picture http://img340.imageshack.us/i/correctorderfrompreview.jpg/   We therefore deploy the report and open it up in Internet Explorer but now the order of the legend has changed and is shown as (Intdis, Not specified, E2B). See the picture http://img816.imageshack.us/i/incorrectorderfromie.jpg/   We have tried to set the sorting also within the map (not only within the SQL) but the same problem remained although we are not sure we set that sorting correctly . We are not sure that we found the series collection where we could set the sort order. It is all a bit confusing when working with the maps.   As a final solution we tried to change the available values in “ReportFormat” but then the order was even more confusing. See table below; Available values (ReportFormat) Order in legend in Visual studio preview Order in legend in Internet

SSRS2008 Sort columns by row Subtotals

Hi! Have problem with sort some data report. Use Tablix in report, where have Total, Month, Subtotal, Week, Day as ROWS and City as COLUMN. In data cell there is SaleAmount. So in Subtotal and Total there is SUM(SaleAmount) for every City - properly view because of grouping. I'm wondering how to make parameter for sorting data in this report, where have to choose 1.Sort WeekSale 2.Sort MonthSale greets Tomek

order by sort both select in union



I have DropDownList that Show all groups. in that, I want to show "select one group" as default in DDL. for that i used code below. but it sort all return data and "select one group" will not be the first line. How can i fix it?

Best Regards. Morteza


	SELECT   -1 as id,'select one group' as groupName
	SELECT   id, groupName
	FROM     [group] 
	order by groupNam

SSRS 2005 and Sort Order

I have a table and want to sort the 1st column which is called "WorkType" in a particular order. At present it looks like this.

Admin Tasks
Leave & Public Holiday

managed metadata sort order for swedish


Sort order for managed metadata is not as expected, for instance Swedish letters Å and Ä are sorted as A

The properties for the standard language is set to swedish and explicit sort order is not set.

Is this a bug, or are we doing something wrong?


Microsoft SharePoint Server 2010 (14.0.4763.1000)

Language Pack for SharePoint, Project Server and Office Web Apps 2010 - Swedish/svenska (14.0.4763.1000)

Sort WPF Datagrid using multiple columns



Is it possible to sort a WPF datagrid using more than one column ?



Change order of columns in a list


Hi there

I'm looking for a way to change the order of the colums in a list in SharePoint 2010. I only have access to SharePoint Designer 2010 for editing the site.

I already looked at the function to create custom views for lists. However this are only useful when viewing the list as a whole, however when you edit an existing, or create a new entry you will see

  • first: the columns in the order in which they were created.
  • and second: all of the columns. i cant find a way to make certain columns invisible for the user.
I already looked through the functions that are given to me by SharePoint Designer 2010 when editing the columns of a list and I also checked for a way to change the order of the columns when surfing the actual website with Internet Explorer.

Does anyone know how to do this? Or am I just trying to find a function that doesn't make sense because it's covered by another feature?

Thanks for your help!

Persist sort order in datagrid



I've got multiple datagrids in the application I'm currently working on. Their ItemSource can be updated with new content on server notifications. When this happens, the ItemSource is being replaced by a new one (would it be better to update the content of the ItemSource - which is much more complicated for this case of figure - instead of binding a new one?)

When the new ItemSource is being bound, the sorting of the grid is being lost. I've solved this behaviour by saving the sorting before setting the new binding and restoring it after the new binding has been done. Below you'll find the static methods used for that:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Data;
using Telecontrol.SIPPR.Client.Commons.DataObjects;

namespace Telecontrol.SIPPR.Client.Commons.Utils
  public static class DataGridUtil

Sort Display Columns



I want to be able to sort the display columns in the insert/edit screen in Dynamic Data.  I'm using VS 2010.

I have a the following structure:

Table:  Mentor


Column:  PersonId references Person Table(LastName, FirstName, MiddleName)

Column: AddressId references Address Table(Street, City, State, ZipCode)


I'm trying to insert a mentor and the mentor screen shows up with Address first then Person.  How can I code it with Person data showing first?

Here's the Mentor.cs:

    public partial class Mentor
        partial void OnValidate(ChangeAction changeAction)



    public partial class MentorMetaData
        [Display(Order = 0, GroupName = "Personal Information")]
        [UIHint("Person", null, "persontype", "P")]

SharePoint 2010 library sort order is incorrect.


When sorting by name in a SharePoint 2010 library, numeric results are sorting incorrectly. For instance "122_Test" will sort in front of "13_Test". I know at one point years ago Windows Explorer had the same issue, but when viewing the same library by using the Open with Explorer option, Windows Explorer correctly displays the results. Has anyone encountered this issue, and if so have you found a fix?


Tim Vander Kooi

columns order reverses in matrix when exported to excel


Hi eb

I haver a simple matrix with header that holds description for every column

in matrix. Lets say header looks like:

col1Desc col2Desc col3Desc.

My problem: when report is exported to pdf or excel columns order reverses! so now it is:col3Desc col2Desc col1Desc

The only workaround i found to keep columns headres in place was to place header in a table on top of matrix.

This is fine but now matrix itself has no header so i can't display the columns header in every page of the report just on the first one..

I would very much appriciate a hint for solving any of these problems

either prevent columns reverse in matrix header when report is exported

or some how force the column to appear on top of each page

when contained in a separate table.

Thanks alot!




In RB 2.0 how to sort in ascending/descending order as default?

I have column say of Provider Type which I want to be ordered when report is run so that all other columns are ordered according to the order of the Provider Type values.

WPF: how to make a GridView only sort on one column as ascending order?


We use GridView inside the Listview. We need the grid view alwasy sort on one column ascending order and not allow to click column header to change it.

Is there a easy way to achieve it in Xaml?

If not possible, how to achieve it in code behind?



ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend