Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a select query which returns time slot as per the requirement but it comes in numeric order and i want it to be ordered in A.M. to P.M. format.
HEre is my query ..
SQL
select distinct timings from BAT.FacultyTiming_Master fm
where fm.Faculty_Name='Satendra Kumar Jangra' and fm.bookstatus='false'

which returns :

VB
1:00 PM - 2:30 PM
10:00 AM - 11:30 PM
11:30 AM - 1:00 PM
2:30 PM - 4:00 PM
4:00 PM - 5:30 PM
5:30 PM - 7:00 PM
7:00 PM - 8:30 PM
8:30 PM - 10:00 PM

but i want it like ..

VB
10:00 AM - 11:30 PM
11:30 AM - 1:00 PM
1:00 PM - 2:30 PM
2:30 PM - 4:00 PM
4:00 PM - 5:30 PM
5:30 PM - 7:00 PM
7:00 PM - 8:30 PM
8:30 PM - 10:00 PM
Posted
Comments
Richard MacCutchan 14-Apr-14 9:45am    
If you are storing these values as strings then the results you see are correct. They should be stored as DateTime types so they are sorted according to their time value.

1 solution

You can do something like this, but I would rather suggest you to look at you database design and store the value as proper types instead of string values.

CREATE TABLE #Timings (START NVARCHAR(100), ENDS NVARCHAR(100), Timing NVARCHAR(100))

INSERT #Timings
SELECT 
	SUBSTRING(Timings, 1, CHARINDEX('-', Timings) - 1),
	SUBSTRING(Timings, CHARINDEX('-', Timings) + 1, LEN(Timings)),
	Timings
FROM 
	FacultyTiming 
ORDER 
	BY Timings

SELECT 
	Timing
FROM 
	#Timings 
ORDER BY 
        CONVERT(time , START)

DROP TABLE #Timings
 
Share this answer
 

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