Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date sorting

Status
Not open for further replies.

adamroof

Programmer
Nov 5, 2003
1,107
US
Maybe its the way its inserting the dates, but all three methods of sorting all put jun 1st in between april and may...
maybe the governments up to somethin here?

any rules on getting this sorting the right way using convert to a varchar?

Code:
DECLARE @stop int,@sql varchar(800)

--DROP TABLE #dateSort
CREATE TABLE #dateSort
	(date1 varchar(50),date2 varchar(50),date3 varchar(50))

SET @stop = 0
WHILE @stop < 10
     BEGIN
	INSERT INTO #dateSort
	SELECT 	getDate()+(5 * @stop),
		getDate()+(5 * @stop),
		getDate()+(5 * @stop)
	SET @stop = @stop + 1
     END

SELECT * FROM #dateSort
ORDER BY CONVERT(varchar(10),date1,101)

SELECT * FROM #dateSort
ORDER BY CONVERT(varchar(10),date2,102)

SELECT * FROM #dateSort
ORDER BY CONVERT(varchar(10),date3,120)

SELECT * FROM #dateSort
 
If you're only storing date values in those fields, actually create the fields as datetime data types. Then you can sort them reliably and just output the converted format (though the conversion codes 102 and 120 should be sorting them correctly since they return the data in yyyymmdd order).
 
you're right, i meant to insert as smalldatetime, i changed that now all methods are sorting right, but im trying to recreate my earlier prob where it sorted by day first
 
Sort by day first happens when CONVERT() format is dmy (UK/EU and Kuwaiti styles - 103-106, 130-131).

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top