Hicreate table #t (c varchar(50))insert into #t values ('Apr 2007')insert |
Uri Dimant posted on Tuesday, June 26, 2007 8:31 AM
|
Hi
create table #t (c varchar(50))
insert into #t values ('Apr 2007')
insert into #t values ('June 2008')
insert into #t values ('Sep 2007')
insert into #t values ('Nov 2007')
insert into #t values ('Feb 2006')
select * from #t order by convert(datetime,c,103) asc |
 |
|
farshad,Assuming that the character strings are _always_ valid month year |
Russell Fields posted on Tuesday, June 26, 2007 8:39 AM
|
farshad,
Assuming that the character strings are _always_ valid month year
combinations, you could do the following:
SELECT contractMonth
FROM table1
ORDER BY CAST (contractMonth AS DATETIME)
Of course, I would not trust all my contractMonth values to be valid unless
some process was already assuring that. A better method would be to make
the contractMonth a datetime column and just make them all fall on the first
day of the month. It is better to leverage the native capabilities of the
database than try to fool with less well defined data.
RLF |
 |
|
If the data can not be converted to datetime (for example Sep andSeptember are |
Roy Harvey posted on Tuesday, June 26, 2007 9:16 AM
|
If the data can not be converted to datetime (for example Sep and
September are fine, but Sept will not convert) you may have to resort
to extreme measures, perhaps something like:
ORDER BY CASE WHEN contractMonth LIKE '%2000%' THEN 2000
WHEN contractMonth LIKE '%2001%' THEN 2001
WHEN contractMonth LIKE '%2002%' THEN 2002
WHEN contractMonth LIKE '%2003%' THEN 2003
WHEN contractMonth LIKE '%2004%' THEN 2004
WHEN contractMonth LIKE '%2005%' THEN 2005
WHEN contractMonth LIKE '%2006%' THEN 2006
WHEN contractMonth LIKE '%2007%' THEN 2007
WHEN contractMonth LIKE '%2008%' THEN 2008
WHEN contractMonth LIKE '%2009%' THEN 2009
END,
CASE WHEN contractMonth LIKE '%jan%' THEN 1
WHEN contractMonth LIKE '%feb%' THEN 2
WHEN contractMonth LIKE '%mar%' THEN 3
WHEN contractMonth LIKE '%apr%' THEN 4
WHEN contractMonth LIKE '%may%' THEN 5
WHEN contractMonth LIKE '%jun%' THEN 6
WHEN contractMonth LIKE '%jul%' THEN 7
WHEN contractMonth LIKE '%aug%' THEN 8
WHEN contractMonth LIKE '%sep%' THEN 9
WHEN contractMonth LIKE '%oct%' THEN 10
WHEN contractMonth LIKE '%nov%' THEN 11
WHEN contractMonth LIKE '%dec%' THEN 12
END
Roy Harvey
Beacon Falls, CT
On Tue, 26 Jun 2007 05:19:02 -0700, farshad |
 |
|
... |
Raymond D'Anjou posted on Tuesday, June 26, 2007 9:24 AM
|
...order by right(contractMonth, 4), contractMonth |
 |
|
Hi RoyIn my opinion it would be better to 'fix' the data to be converted to |
Uri Dimant posted on Tuesday, June 26, 2007 9:26 AM
|
Hi Roy
In my opinion it would be better to 'fix' the data to be converted to
datetime insead of building so long long query. Image if he has
to sort contracts from 1950.....
Just my two cents |
 |
|
order |
Roy Harvey posted on Tuesday, June 26, 2007 10:23 AM
|
On Tue, 26 Jun 2007 16:26:47 +0300, "Uri Dimant" <urid@iscar.co.il>
Of course it would be better to fix the data, if fixing the data (and
KEEPING it fixed!) is an option. Of course a real fix would be to get
away from storing 'June 2008' and 'Sep 2007' in the first place and
use datetime.
Roy Harvey
Beacon Falls, CT |
 |
|
Forget this. |
Raymond D'Anjou posted on Tuesday, June 26, 2007 10:53 AM
|
Forget this.
It was a holiday weekend in Quebec and I may have had a bit too much brew.
I should be back to normal tomorrow. |
 |
|