Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to use order by in sql query
-------------------------------

here am using
SELECT
DISTINCT
Where

and I want to use Order by clause in this query. to display SCH_TIME(Schedule Time) in Ascending order.


This is my code.

C#
SqlCommand cmd = new SqlCommand("Select DISTINCT Flightno,DESCR,substring(SCH_TIME,10,4) as ScheduleTime,Destination,Status,Gate from DFlights where(INT_DOM='I' and ARR_DEP='D')", con);


Please can you help me.

your help is appreciated.

Thanks.
Posted
Comments
Monicavensaslas 27-Mar-15 4:04am    
Hope this will help you.. :)

SqlCommand cmd = new SqlCommand("Select DISTINCT Flightno,DESCR,substring(SCH_TIME,10,4) as ScheduleTime,Destination,Status,Gate from DFlights where(INT_DOM='I' and ARR_DEP='D') order by SCH_TIME asc", con);




SqlCommand cmd = new SqlCommand("Select DISTINCT Flightno,DESCR,substring(SCH_TIME,10,4) as ScheduleTime,Destination,Status,Gate from DFlights where(INT_DOM='I' and ARR_DEP='D') order by substring(SCH_TIME,10,4)", con);


order by clause should always place after where clause
 
Share this answer
 
Hi,

Check this...


SQL
Select DISTINCT Flightno,
DESCR,
substring(SCH_TIME,10,4) as ScheduleTime,
Destination,
Status,
Gate 
from 
DFlights 
where
INT_DOM='I' and ARR_DEP='D'
Order By 3


Hope this will help you.

Cheers
 
Share this answer
 
You can also do the same using following format

SqlCommand cmd = new SqlCommand("Select DISTINCT Flightno,DESCR,substring(SCH_TIME,10,4) as ScheduleTime,Destination,Status,Gate from DFlights where(INT_DOM='I' and ARR_DEP='D') order by 3", con);
 
Share this answer
 
You can write like this
SqlCommand cmd = new SqlCommand("Select DISTINCT Flightno,DESCR,substring(SCH_TIME,10,4) as ScheduleTime,Destination,Status,Gate from DFlights where INT_DOM='I' and ARR_DEP='D' order by 3", con);
 
Share this answer
 
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900