Home All Groups Group Topic Archive Search About
Author
8 Dec 2005 5:16 PM
Manny Chohan
Hi,

I have column with values January, February and so on. I need to perform
sort based on months instead the system sorts it by Alphabetical Order. Any
Hint?

Thanks

Manny

Author
8 Dec 2005 5:30 PM
David Portas
Manny Chohan wrote:
> Hi,
>
> I have column with values January, February and so on. I need to perform
> sort based on months instead the system sorts it by Alphabetical Order. Any
> Hint?
>
> Thanks
>
> Manny

How about storing dates as DATETIME / SMALLDATETIME rather than
strings? If it's too late to do that then you can try:

SELECT mth
FROM tbl
ORDER BY CONVERT(DATETIME,mth+' 01 2000',1) ;

--
David Portas
SQL Server MVP
--
Author
9 Dec 2005 7:22 PM
Joe from WI
David with all due respect, I'd suggest a datetime format that carries the
century.  Yes, you specify the year 2000 but we should all be developing code
that doesn't leave ambiguity because you never know what SQL Server defaults
will be in the future and more impportantly how your technique will be
applied to other situations.
    CONVERT(DATETIME,mth+' 01 2000',101)  --mm/dd/yyyy format

And, we should be thinking globally so it really should be:
    CONVERT(DATETIME,mth+' 01 2000',112)   --yyyymmdd format

Just my two cents,
Joe

Show quote
"David Portas" wrote:

> Manny Chohan wrote:
> > Hi,
> >
> > I have column with values January, February and so on. I need to perform
> > sort based on months instead the system sorts it by Alphabetical Order. Any
> > Hint?
> >
> > Thanks
> >
> > Manny
>
> How about storing dates as DATETIME / SMALLDATETIME rather than
> strings? If it's too late to do that then you can try:
>
> SELECT mth
>  FROM tbl
>  ORDER BY CONVERT(DATETIME,mth+' 01 2000',1) ;
>
> --
> David Portas
> SQL Server MVP
> --
>
>
Author
9 Dec 2005 7:35 PM
David Portas
Joe from WI wrote:
> David with all due respect, I'd suggest a datetime format that carries the
> century.  Yes, you specify the year 2000 but we should all be developing code
> that doesn't leave ambiguity because you never know what SQL Server defaults
> will be in the future and more impportantly how your technique will be
> applied to other situations.
>     CONVERT(DATETIME,mth+' 01 2000',101)  --mm/dd/yyyy format
>

A good point.


> And, we should be thinking globally so it really should be:
>     CONVERT(DATETIME,mth+' 01 2000',112)   --yyyymmdd format
>

That does work although on the face of it the string is wrong. 112
defines the format as YYYYMMDD, which is not what you have specified.
There is some implict conversion at work here so I'd stick to the 101
version because the string that's passed complies with the documented
format and behaviour for CONVERT.

--
David Portas
SQL Server MVP
--
Author
8 Dec 2005 5:33 PM
Stijn Verrept
Manny Chohan wrote:

> I have column with values January, February and so on. I need to
> perform sort based on months instead the system sorts it by
> Alphabetical Order. Any Hint?


Create a table with months:

Table Months
ID int NOT NULL,
MonthName varchar(20)

and do a join on that.  Normally it would be better to save the month
ID instead of the full text.

--

HTH,

Stijn Verrept.
Author
8 Dec 2005 5:35 PM
Trey Walpole
do you not have a real date? if not, then do you at least have a year as
well? if not, sorting my month number is pretty meaningless. (e.g. Jan
05 comes after Dec 04).


Manny Chohan wrote:
Show quote
> Hi,
>
> I have column with values January, February and so on. I need to perform
> sort based on months instead the system sorts it by Alphabetical Order. Any
> Hint?
>
> Thanks
>
> Manny
Author
9 Dec 2005 12:45 AM
ML
I apologize for the off-topic comment, but this one is just too good to pass:

> do you not have a real date?

How many times have I been asked that question - not necesarily in the same
context, but still... :)


ML

---
http://milambda.blogspot.com/
Author
8 Dec 2005 5:45 PM
Steve Kass
.... order by
charindex(monthnamecol+'*','January*February*March*April*May*June*July*August*September*October*November*December*')

Steve Kass
Drew University

Manny Chohan wrote:

Show quote
>Hi,
>
>I have column with values January, February and so on. I need to perform
>sort based on months instead the system sorts it by Alphabetical Order. Any
>Hint?
>
>Thanks
>
>Manny

>
Author
8 Dec 2005 6:48 PM
Manny Chohan
Thanks. It worked.

Show quote
"Steve Kass" wrote:

>
> .... order by
> charindex(monthnamecol+'*','January*February*March*April*May*June*July*August*September*October*November*December*')
>
> Steve Kass
> Drew University
>
> Manny Chohan wrote:
>
> >Hi,
> >
> >I have column with values January, February and so on. I need to perform
> >sort based on months instead the system sorts it by Alphabetical Order. Any
> >Hint?
> >
> >Thanks
> >
> >Manny
> > 
> >
>

AddThis Social Bookmark Button