Wednesday, March 21, 2012

ORDER BY <VarChar Field>

Hi group,

I've got a table with two columns named [Year] and [Month]. They are both defined as VarChar.

Question:
Is it possible to ORDER THEM as if they where of type DateTime?

EG
select [year], [month]
from tbl_WeightedAverageGenerated
where [Year] = 2006
ORDER BY [Month]

Returns:
2006, 10
2006, 11
2006, 12
2006, 5
2006, 6
etc...

I need it to return:
2006 5
2006 6
2006 7
2006 8
2006 9
2006 10
2006 11
2006 12

Is this possible....and how?

TIA

Regards,

SDerix

Yes, cast them as integers first.

select [year], [month]
from tbl_WeightedAverageGenerated
where [Year] = 2006
ORDER BY cast([Month] as int)

-Jamie

No comments:

Post a Comment