If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > help date will not order by desc

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-30-03, 12:42
wadecarlson wadecarlson is offline
Registered User
 
Join Date: Sep 2003
Posts: 6
help date will not order by desc

Why is october at the bottom of the list?

sql code:
SELECT userInfo.*, marks.*, marks.datefield FROM userInfo INNER JOIN marks ON userInfo.userID = marks.userID ORDER BY marks.datefield desc;

this is what i get:
9/30/2003
9/23/2003
9/19/2003
9/1/2003
8/25/2003
8/21/2003
8/19/2003
8/19/2003
10/16/2003
10/1/2003

Why is october at the bottom of the list?
Reply With Quote
  #2 (permalink)  
Old 10-30-03, 14:23
evanhattem evanhattem is offline
Registered User
 
Join Date: Sep 2003
Location: The Netherlands
Posts: 311
hi,

alphabeticly the 9 is higher then the 1. Like any other tool/rdbms oracle sort character by character and 9 is higher than 1. U have to format the dates before sorting to make sure that oracle compares 10 to 9 instead of 1 to 9. Do so by formatting the date like:

select to_char(sysdate,'MM/DD/YYYY') from dual.

Then u get '09/12/2003'. Then compare it to the other dates and the sorting will be good.

Hope this helps
__________________
Edwin van Hattem
OCP DBA / System analyst
Reply With Quote
  #3 (permalink)  
Old 10-30-03, 14:33
arin_am arin_am is offline
Registered User
 
Join Date: Jun 2003
Posts: 34
Quote:
Originally posted by evanhattem
hi,

alphabeticly the 9 is higher then the 1. Like any other tool/rdbms oracle sort character by character and 9 is higher than 1. U have to format the dates before sorting to make sure that oracle compares 10 to 9 instead of 1 to 9. Do so by formatting the date like:

select to_char(sysdate,'MM/DD/YYYY') from dual.

Then u get '09/12/2003'. Then compare it to the other dates and the sorting will be good.

Hope this helps
Am bit confused here I tried reproducing this problem but never got what is shown here. I tried the table with date field and also varchar field both ways the DESC worked.
Reply With Quote
  #4 (permalink)  
Old 10-30-03, 15:03
wadecarlson wadecarlson is offline
Registered User
 
Join Date: Sep 2003
Posts: 6
Quote:
Originally posted by evanhattem
hi,

alphabeticly the 9 is higher then the 1. Like any other tool/rdbms oracle sort character by character and 9 is higher than 1. U have to format the dates before sorting to make sure that oracle compares 10 to 9 instead of 1 to 9. Do so by formatting the date like:

select to_char(sysdate,'MM/DD/YYYY') from dual.

Then u get '09/12/2003'. Then compare it to the other dates and the sorting will be good.

Hope this helps

select to_char(sysdate,'MM/DD/YYYY') from dual.
could you describe what to_char stand for? or use my code to make it work? i am at a loss. thanks for replying!
Reply With Quote
  #5 (permalink)  
Old 10-30-03, 15:13
wadecarlson wadecarlson is offline
Registered User
 
Join Date: Sep 2003
Posts: 6
Quote:
Originally posted by wadecarlson
select to_char(sysdate,'MM/DD/YYYY') from dual.
could you describe what to_char stand for? or use my code to make it work? i am at a loss. thanks for replying!

i just changed the data type in access to date/time and this seemed to fix the glitch.

thx all!!!!!
Reply With Quote
  #6 (permalink)  
Old 10-30-03, 15:15
mkkmg mkkmg is offline
Registered User
 
Join Date: Oct 2003
Location: Dallas
Posts: 76
....

basically what he is saying is to format the date differently.

instead of viewing 9/1/2003 change the format to 09/1/2003, with placing the 09 instead of 9 it will order properly.

just format the date column
Reply With Quote
  #7 (permalink)  
Old 10-31-03, 01:03
evanhattem evanhattem is offline
Registered User
 
Join Date: Sep 2003
Location: The Netherlands
Posts: 311
Quote:
Originally posted by wadecarlson
select to_char(sysdate,'MM/DD/YYYY') from dual.
could you describe what to_char stand for? or use my code to make it work? i am at a loss. thanks for replying!
Oracle knows only one format for the internal storing of a date. But we can show it differently. Internally it would be like mm/dd/yyyy, but we can show it like DD-Month-YY. To do so, we have to convert the date to a character string, because Oracle knows only one format of the date internally. TO_CHAR changes a date or a number value to a character, allowing us to display it in different format.

Good thing u have got it working.

Greetz.
__________________
Edwin van Hattem
OCP DBA / System analyst
Reply With Quote
  #8 (permalink)  
Old 10-31-03, 06:58
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
you still have a sorting problem, which you will discover in january

Reply With Quote
  #9 (permalink)  
Old 11-04-03, 10:53
wadecarlson wadecarlson is offline
Registered User
 
Join Date: Sep 2003
Posts: 6
Quote:
Originally posted by r937
you still have a sorting problem, which you will discover in january

what do you mean? what will happen in jan?
Reply With Quote
  #10 (permalink)  
Old 11-04-03, 12:25
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
*********************************
*********************************
W00H00!!! my 1000th dbforums posting!
*********************************
*********************************


in january, if you are still using TO_CHAR to format and sort your dates, you will find that 01/01/2004 comes ahead of 12/31/2003 because you are sorting them as character strings


rudy
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On