DeveloperBarn Forums

Go Back   DeveloperBarn Forums > Databases > SQL Development

Discuss "ordering problem" in the SQL Development forum.

SQL Development - Structured Query Language (SQL) is the talk of databases. Discuss topics such as joins, triggers and other advanced SQL topics.


Closed Thread
 
LinkBack (1) Thread Tools Display Modes
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 04-17-2008, 03:10 PM
Contributing Member
 
Join Date: Mar 2008
Posts: 124
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1
todd2006 is an unknown quantity at this point
Default ordering problem

Hi,

I dont want to display the current topic and i want to display the topics starting from 2008 and then going to 2000 or 1999

so i am sorting on topic date but its not working any idea

Code:
sqlstr="SELECT * FROM Topics WHERE (Topic_Date <> (SELECT TOP 1 Topic_Date FROM Topics WHERE Topic_Date <= getDate() ORDER BY Topic_Date desc)) order by Topic_Date desc"
when the topics are displayed they start at 2007 year and then 2008 and then 2006 and so on

todd
Sponsored Links
  #2 (permalink)  
Old 04-17-2008, 04:29 PM
Contributing Member
 
Join Date: Mar 2008
Posts: 124
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1
todd2006 is an unknown quantity at this point
Default

topic_date is a varchar field
  #3 (permalink)  
Old 04-18-2008, 07:57 AM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 397
Thanks: 12
Thanked 20 Times in 20 Posts
Rep Power: 1
jmurrayhead is on a distinguished road
Default

Quote:
Originally Posted by todd2006 View Post
topic_date is a varchar field
That's your problem. Dates should be stored in a datetime field or things like this could happen.
__________________
jmurrayhead
Did I help you out? Make me popular by clicking the icon!

If you found a post helpful, please click the button in the lower right-hand corner of the post.

Powered by ASP.Net
  #4 (permalink)  
Old 04-18-2008, 09:14 AM
Wolffy's Avatar
Slaprentice of Wolves

 
Join Date: Mar 2008
Location: Peoria, IL
Posts: 91
Thanks: 1
Thanked 15 Times in 12 Posts
Rep Power: 1
Wolffy is on a distinguished road
Default

I agree -- however even a character 4-digit year should sort correctly. I wonder about the condition Topic_Date <= getDate(), which compares a VARCHAR with a DATE value. I'm surprised the query even parses. Beyond that, since date/times are nothing more than integers internally, I'm thinking that this compare isn't doing what you think it is. Even if the analyzer figure it out, it would be something like saying '2008' <= '04/15/2008 12:00:00 AM'
  #5 (permalink)  
Old 04-18-2008, 09:55 AM
Contributing Member
 
Join Date: Mar 2008
Posts: 124
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1
todd2006 is an unknown quantity at this point
Default

Hi,

this works out

Code:
sqlstr="SELECT * FROM Topics WHERE (Convert(datetime,Hot_Topic_Date) <> (SELECT TOP 1 Convert(datetime,Hot_Topic_Date) FROM Topics WHERE Convert(datetime,Hot_Topic_Date) <= getDate() ORDER BY Convert(datetime,Hot_Topic_Date) desc)) order by Convert(datetime,Hot_Topic_Date) desc"
  #6 (permalink)  
Old 04-18-2008, 09:58 AM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 397
Thanks: 12
Thanked 20 Times in 20 Posts
Rep Power: 1
jmurrayhead is on a distinguished road
Default

I didn't even think of using the CONVERT function. Glad you got it sorted. Please use the thread tools menu and mark your thread as solved
  #7 (permalink)  
Old 04-18-2008, 12:12 PM
lewy's Avatar
Dark Sonic Master

 
Join Date: Mar 2008
Location: South TX
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1
lewy is on a distinguished road
Default

I would seriously consider changing your data type to DateTime

It would avoid needless conversions

Comments on this post
BLaaaaaaaaaarche agrees: Yep, I agree.
__________________
................... ASCII and ye shall receive ..................
Knowledge is the only resource on earth that multiplies when shared

If you found a post helpful, please click the 'Thanks' button in the lower right-hand corner of the post.
Closed Thread

  DeveloperBarn Forums > Databases > SQL Development

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.developerbarn.com/sql-development/133-ordering-problem.html
Posted By For Type Date
DeveloperBarn Forums - ASP Help, ASP.Net Help, PHP Help, SQL Help, Tutorials, Windows Help This thread Refback 04-26-2008 09:36 AM


Sponsored Links

ASP.NET Resource Index
a directory of ASP.NET tutorials, applications, scripts, assemblies and articles for the novice to professional developer.

Free Web Directory
Including Chats and Forums Resources, Offer automatic, instant and free directory submissions.
URLZ Web Directory
URLZ Web Directory

Free Web Directory - Add Your Link
The Little Web Directory
Free Web Directory
Pegasus free web directory is a free directory organised by categories.


All times are GMT -4. The time now is 12:55 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0 RC7
©2008 DeveloperBarn.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45