Click here to Skip to main content
Click here to Skip to main content

Sorting column that contains string with numeric value

, 18 Nov 2011
Rate this:
Please Sign up or sign in to vote.
Sorting column that contains string with numeric value
You must have to concern data sorting when you work with wide range of data source. Well nothing to say more about shorting which is very important. Okay let’s consider the following scenario:
You have a table with few columns & you will sort the table based on a specific column that contains numeric value with string type value. A sample table is given below:
 
Table# tempTable
 
c1 c2
 
1 368 Mirpur
2 243 Gulshan
3 101 DOHS
. …………..
etc.,
 
So now, you have to sort the table base on c2, how can you do that? It’s very simple, to achieve this we will use PATINDEX() function. The following query will resolve the issue.
 
Example:
 
DECLARE @tempTable TABLE (id INT, String_WithN_umericValue VARCHAR(100))
INSERT INTO @tempTable VALUES (1,’7th’)
INSERT INTO @tempTable VALUES(2,’4th’)
INSERT INTO @tempTable VALUES(3,’9th’)
INSERT INTO @tempTable VALUES(4,’10th’)
INSERT INTO @tempTable VALUES(5,’2nd’)
INSERT INTO @tempTable VALUES(6,’5th’)
INSERT INTO @tempTable VALUES(7,’8th’)
INSERT INTO @tempTable VALUES(8,’1st’)
INSERT INTO @tempTable VALUES(9,’3rd’)
INSERT INTO @tempTable VALUES(10,’6th’)
 
SELECT * FROM @tempTable ORDER BY CAST(LEFT(String_WithN_umericValue,PATINDEX(‘%[^0-9]%’,String_WithN_umericValue)-1) AS INT)
 
More information about PATINDEX() function can be found at this link.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Md. Marufuzzaman
CEO
Bangladesh Bangladesh
A highly experienced leader with successful track record of brand management, product innovations and corporate communication. Some successful product innovations have also achieved and awards “Most Valuable Professional” (MVP) at 2010 and 2011 by CodeProject.com and also selected as a mentor of CodeProject.com
 
Over ten years of professional experiences in ICT field having extensive experience in formulating corporate vision and long term strategy. Leading development related functions including design, services, data management and analytics, customer experience management, content services, digital analytics and optimization.
 
An individual with results-driven approach and relentless in pursuit of excellence from a business and organizational standpoint. Believes in transparency, commitment and teamwork.
 
Specialties: Developing client-server with multi-tier application, database design, web based business software solutions, enterprise applications, google analytics, etc., for Windows / Mac using Microsoft SQL Server, Oracle, MySql, PS, Visual C#, R, VB.NET, ASP.NET, PHP, API, MVC, WebAPI , Add-In Visual Basic etc,. I have also more than two years’ of strong experience in mobile-VAS (platform development).
Follow on   Twitter   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 18 Nov 2011
Article Copyright 2011 by Md. Marufuzzaman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid