Search EggHeadCafe's Job Board
EggHeadCafe Silverlight WPF ASP.NET VB.NET C# Excel SQL Server SharePoint
search
SQL Server GroupsView
SQL Server Ce
SQL Server Clients
SQL Server Connect
SQL Server Datamining
SQL Server Datawarehouse
SQL Server Dts
SQL Server Fulltext
SQL Server Msde
SQL Server Olap
SQL Server Replication
SQL Server Reportingsvcs
SQL Server Security
SQL Server Server
SQL Server Setup
SQL Server Tools
SQL Server Clustering
SQL Server Programming
SQL Server Xml
SQL Server Newusers
SQL Server Integrationsvcs

Group SummariesView
.NET Framework
Access
BizTalk
Certifications
CRM
DDK
Exchange Server
FoxPro
French
French .NET
Games
German
German .NET
Graphic Design
IIS
Internet
ISA Server
Italian
Italian .NET
Maps
MCIS
Miscellaneous
Mobile Apps
Money
MSN
Networking
Office
Ops Mgr
Publisher
Security
SharePoint
Small Business
Spanish
Spanish .NET
SQL Server
Systems Management Server
Transaction Server
Virtual PC / Virtual Server
Visual Studio
Win32
Windows 2000
Windows 2003 Server
Windows 7
Windows Live
Windows Media
Windows Update
Windows Vista
Windows XP
 

View All Microsoft SQL Server Programming Posts  Ask A New Question 

order

farsha posted on Tuesday, June 26, 2007 8:19 AM

Hi,
There is a field called contractMonth in a table called table1.
The datatype of this field is varchar(50).
The data is something like:

Apr 2007
June 2008
Sep 2007
...

How is it possible to order this field.
For example I would like to retrieve something like the following:
Apr 2007
sep 2007
June 2008
...

Thanks
reply

 

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
reply

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
reply

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
reply

...

Raymond D'Anjou posted on Tuesday, June 26, 2007 9:24 AM

...order by right(contractMonth, 4), contractMonth
reply

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
reply

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
reply

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.
reply


Previous Microsoft SQL Server Programming conversation.