Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > Sybase > sort by month

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-05-07, 02:53
shiya shiya is offline
Registered User
 
Join Date: Mar 2007
Posts: 29
sort by month

Hi Friends,

I have a table month with name of the month as column values. I want to sort them by month. How can I do that in sybase? Please help me.

create table month(name varchar(10))

select * from month

name
----------
Jan
Feb
Mar
May
Jul
Apr
Dec

I want to sort them by month so that my output should look like..

name
----------
Jan
Feb
Mar
Apr
May
Jul
Dec

Thanks in advance,
Shiya
Reply With Quote
  #2 (permalink)  
Old 12-05-07, 06:23
trvishi trvishi is offline
Registered User
 
Join Date: Sep 2003
Location: Switzerland
Posts: 371
Just having Jan,Feb etc and trying to sort wont work because it will do a character sort.

Suggest having numbers along with it.
i.e.

create table month(mth int, mth_str char(3))
insert into month values(1,'Jan')
etc
and then use order by mth.

Also, not sure what the larger scope of your issue is. There are a lot of date functions which you can use along with maybe a date field. Check sybooks.sybase.com and use the reference manual/functions.
Reply With Quote
  #3 (permalink)  
Old 12-05-07, 10:05
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 830
order by convert(datetime,'1 '+name+' 2000')

-- OR --

order by case name
when 'Jan' then 1
when 'Feb' then 2
when 'Mar' then 3
-- etc.
when 'Dec' then 12
else 13
end
Reply With Quote
  #4 (permalink)  
Old 12-05-07, 10:59
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,555
Code:
order by charindex(name,'DecNovOctSepAugJulJunMayAprMarFebJan') desc
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #5 (permalink)  
Old 12-05-07, 18:39
trvishi trvishi is offline
Registered User
 
Join Date: Sep 2003
Location: Switzerland
Posts: 371
Quote:
Originally Posted by r937
Code:
order by charindex(name,'DecNovOctSepAugJulJunMayAprMarFebJan') desc
very nice
Reply With Quote
  #6 (permalink)  
Old 12-06-07, 03:08
shiya shiya is offline
Registered User
 
Join Date: Mar 2007
Posts: 29
Thanks a lot for all the replies

Shiya
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

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