| Author |
Topic |
|
spudhead
Starting Member
34 Posts |
Posted - 2002-07-23 : 10:36:50
|
I'm having some problems with ordering rows from a SQL Server table by date.The table records posts in an online forum, with the post-time logging column set to type "datetime", length of 8. Standard stuff.The stored proc that pulls out forum posts reads:CREATE PROCEDURE forum_showThread@tid intASSELECTforum_posts.id,forum_posts.body,forum_posts.thread_id,CONVERT(varchar(20), forum_posts.post_date) AS post_date,forum_users.username,forum_users.emailFROM forum_posts INNER JOIN forum_users ON forum_posts.user_id=forum_users.idWHERE forum_posts.thread_id=@tid ORDER BY forum_posts.post_date DESCGO An example of what it pulls out (in order) is:Jul 23 2002 11:35AMJul 23 2002 11:33AMJul 23 2002 11:08AMJul 23 2002 3:29PMJul 22 2002 5:12PMJul 22 2002 4:24PMJul 22 2002 4:22PMJul 22 2002 4:00PMJul 22 2002 3:36PMAs you can see, if I do this then it sees 3.29PM on July 23 as an EARLIER date value than 11.08AM on the same day.I have checked this with several clocks and can confirm that this is, in fact, not the case.How can I get round this? If I don't convert the date to a varchar, it comes out in the right order - but I need to convert the date otherwise... well, the date just looks silly and my client will moan.Thanks for any suggestions. |
|
|
macka
Posting Yak Master
162 Posts |
Posted - 2002-07-23 : 10:51:41
|
| Try altering your ORDER BY clause to use the same format as in the SELECT. ie:ORDER BY CONVERT(varchar(20), forum_posts.post_date) DESCmacka. |
 |
|
|
spudhead
Starting Member
34 Posts |
Posted - 2002-07-23 : 11:04:34
|
| No, sorry, that still doesn't work. I guess it's looking at "11", seeing it as higher than "3" and ordering the rows accordingly. Can I pull the date out as a 24hr clock value? |
 |
|
|
sterobhun
Starting Member
12 Posts |
Posted - 2002-07-23 : 11:08:40
|
| Interestingly, this is how they would be ordered if the post_date was being converted to a varchar in the order by clause, because '11' is higher than ' 3'. Are you sure it's a datetime? What happens if you use something other name instead of "as post_date"? |
 |
|
|
M.E.
Aged Yak Warrior
539 Posts |
Posted - 2002-07-23 : 11:10:49
|
| try ordering byconvert(datetime,forum_posts.post_date,13)That converts it over to military time.so instead of ORDER BY forum_posts.post_date DESCuse order by convert(datetime,forum_posts.post_date,13) desc-----------------------Take my advice, I dare ya |
 |
|
|
spudhead
Starting Member
34 Posts |
Posted - 2002-07-23 : 11:15:14
|
| Oh, this is ridiculous.OK, so I changed it to a 24hr date value and...23 Jul 2002 11:35:2223 Jul 2002 11:33:5023 Jul 2002 11:08:2823 Jul 2002 15:29:5122 Jul 2002 17:12:0122 Jul 2002 16:24:0022 Jul 2002 16:22:02It gets the date right (oh, well done SQL Server) but still thinks that 11 in the morning comes after 3 in the afternoon??Please, someone tell me this isn't right. |
 |
|
|
M.E.
Aged Yak Warrior
539 Posts |
Posted - 2002-07-23 : 11:17:24
|
| could you post the exact code that comes up with that.. and could you also tell us which Database your runnning-----------------------Take my advice, I dare ya |
 |
|
|
MuffinMan
Posting Yak Master
107 Posts |
Posted - 2002-07-23 : 12:02:42
|
SQL is ORDERing using the alias you have defined, "post_date", which is a varchar, and thus doesn't sort the way you would expect. Look at these examples.Notice in SELECT #2 that I define the column as p_date and ORDER BY p_date and I get the same sort order as in SELECT #1. SELECT #3 and #4 sort the data correctly.set nocount oncreate table #tmp (Post_Date datetime )insert #tmp (Post_Date) values ('Jul 22 2002 5:12PM')insert #tmp (Post_Date) values ('Jul 23 2002 3:29PM')insert #tmp (Post_Date) values ('Jul 23 2002 11:08AM')insert #tmp (Post_Date) values ('Jul 23 2002 11:33AM')print ''print 'Select #1'select CONVERT(varchar(20), post_date) AS post_datefrom #tmpORDER BY post_date DESCprint ''print 'Select #2'select CONVERT(varchar(20), post_date) AS p_date from #tmpORDER BY p_date descprint ''print 'Select #3'select CONVERT(varchar(20), post_date) AS formatted_post_date from #tmpORDER BY post_date descprint ''print 'Select #4'select CONVERT(varchar(20), post_date) AS post_date from #tmpORDER BY convert(datetime, post_date,13) desc-- drop table #tmp-- here are the results/*********Select #1post_date -------------------- Jul 23 2002 11:33AMJul 23 2002 11:08AMJul 23 2002 3:29PMJul 22 2002 5:12PM Select #2p_date -------------------- Jul 23 2002 11:33AMJul 23 2002 11:08AMJul 23 2002 3:29PMJul 22 2002 5:12PM Select #3formatted_post_date -------------------- Jul 23 2002 3:29PMJul 23 2002 11:33AMJul 23 2002 11:08AMJul 22 2002 5:12PM Select #4post_date -------------------- Jul 23 2002 3:29PMJul 23 2002 11:33AMJul 23 2002 11:08AMJul 22 2002 5:12PM*****/ |
 |
|
|
olily
Starting Member
37 Posts |
Posted - 2002-07-23 : 21:28:42
|
| In your ORDER clause, try to put conver(datetime,forum_posts.post_date,13) as your order field. |
 |
|
|
spudhead
Starting Member
34 Posts |
Posted - 2002-07-24 : 04:41:26
|
| Aha. It works :) Many thanks for all your help, everyone. |
 |
|
|
|