.Net & SharePoint '07

Technical blog for .Net and all SharePoint 2007 related Information

About the author

Me(Prince) and my wife are B.E in I.T & C.S.E respectively.  I a certified MCPD: Web from 2007 Dec. I am Intrestes in Web Application, MOSS, EPM, etc.
Now working with Deira International School, as IT Application & Help Manager. I have started my career as "Software Developer" @  REACH Sewn Technologies and Consulting Pvt. Ltd, Bangalore India from Oct 2004 to Feb 2006, then as "Web & Intranet Developer" @ Fosroc International Ltd, Dubai from April 2006 to Sep 2009.
You can catch me on mail@jpy-tech.com or mail@princepy.com. Or on 00971 - 50 - 4284530 

Google Translate

Tag cloud

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

RecentComments

Comment RSS

Google Your Location


AlphaNumeric Field Split into Alphabet and Numeric And Sort in Sql 2005

I like to do the AlphaNumeric sorting by using the sql select query in sql server 2005

10A
10C
11
12A
17A
1A
2A
4A
.
.
.
etc

I need this list to be as to

1A
2A
4A
10A
10C
11
12A
17A
.
.
.
etc 

For that I need to Split the field in to Numbeic and Alphabet and orderby Alphabet Field, Numeric Field

Alphabet Field = "RIGHT(C.CODE,PATINDEX('%[A-Z]%',REVERSE(C.CODE)))"
Numeric Field = "LEFT(C.CODE,LEN(C.CODE) - PATINDEX('%[0-9]%',C.CODE))"

select
 DISTINCT
 C.CODE AS TXT,
 C.CODE,
 CASE WHEN ISNUMERIC(C.CODE) = 1 THEN CAST(C.CODE AS INT) ELSE CAST(LEFT(C.CODE,LEN(C.CODE) - PATINDEX('%[0-9]%',C.CODE)) AS INT) END AS NumField ,
 CASE WHEN ISNUMERIC(C.CODE) = 0 THEN RIGHT(C.CODE,PATINDEX('%[A-Z]%',REVERSE(C.CODE))) ELSE C.CODE END AS AlphaField
FROM CATEGORY AS C
WHERE (C.CAT_TYPE = 'TRAN' OR C.CAT_TYPE = 'MEAL') AND (C.IN_USE = 'Y')
ORDER BY NumField, AlphaField, CODE

Posted by Admin on Sunday, November 08, 2009 12:27 AM
Permalink | Comments (0) | Post RSSRSS comment feed