473,410 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

Sorting by using parts of a string

Hi,

This is a tougher one, but I'm quite sure someone will have a solution
for this. Of course, a last minute thing thrown at me by my boss.

I have a unique identifier consisting of regions, districts, a road
class value and then sequential numbers, in that order, eg:
GA-GAD-C-005 The thing is that the sequential values don't follow onto
each other as it does the sorting on the road class value first (which
could be A, I or C). So, you'll have the first three parametres
correct, but the number skips values, which appears in a different
"group" for that district. I need to thus sort the roads by ignoring
the road class and only start by sorting it on the region, then
district and then the sequential number. Do I need to do this
programmatically? I'd like to do this in SQL however as most of my
reports and forms run off the SQL statements.

Your help will be GREATLY appreciated.

Dean Carstens

Scott Wilson
DFID Support to Feeder Roads
Ghana

Apr 7 '06 #1
4 1789
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Assuming the unique ID is always
2 chars - 3 chars - 1 char - 3 numerals:

SELECT unique_id, <other cols>
FROM table_name
WHERE ...
ORDER BY Left(unique_id,7) & Right(unique_id,3)

The string concatenation yields a unique_id without the road class;
you'd sort on that.
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBRDaaiIechKqOuFEgEQJYUQCgldN6AgiVWmNJhEavuBA3uy FPR0IAnRoL
y8PT29cwcxet2XxVnEkLgwd9
=p/vx
-----END PGP SIGNATURE-----
de**********@yahoo.com wrote:
Hi,

This is a tougher one, but I'm quite sure someone will have a solution
for this. Of course, a last minute thing thrown at me by my boss.

I have a unique identifier consisting of regions, districts, a road
class value and then sequential numbers, in that order, eg:
GA-GAD-C-005 The thing is that the sequential values don't follow onto
each other as it does the sorting on the road class value first (which
could be A, I or C). So, you'll have the first three parametres
correct, but the number skips values, which appears in a different
"group" for that district. I need to thus sort the roads by ignoring
the road class and only start by sorting it on the region, then
district and then the sequential number. Do I need to do this
programmatically? I'd like to do this in SQL however as most of my
reports and forms run off the SQL statements.

Apr 7 '06 #2
de**********@yahoo.com wrote:
I have a unique identifier consisting of regions, districts, a road
class value and then sequential numbers, in that order, eg:
GA-GAD-C-005 The thing is that the sequential values don't follow onto
each other as it does the sorting on the road class value first (which
could be A, I or C).


Warning - this is how I do it - if anyone sees a better way, for the
love of all that's holy, please dress me down and correct me!!!!

I didn't understand what you were saying after this, but in the example
you give, I'm assuming C is the road class, district is GAD and region
is GA. I'm also going to assume that any of these identifiers can vary
in length (it's easier if they are constant) and that they are always
separated by dashes and that road class will always be *AFTER* the
*SECOND* dash.

The following makes use of the instr() and mid() functions. Look them
up and familiarize yourself with them. The following may be hard to
follow, but it is (one line, no breaks mean to be in place:

mid((mid([Identifier],instr(1,[Identifier], "-")+1)),instr(1,
(mid([Identifier], instr(1,[Identifier], "-")+1) ),"-")+1)

Which will, for the above example, give "C-005" as a result. You can
put this as a calculated expression somewhere in the query builder and
sort by it.

To ease the confusion the above may cause, look at how it's built.

mid takes a portion out of a specified string and instr returns a number.

1. the first instance of a dash: instr(1,[Identifier], "-") will
return the number 3

2. to use this number to get stuff coming after that dash, we want to
take the example and start at position 4, so add one to expression #1
and use it as the start position in mid():

mid([Identifier],instr(1,[Identifier], "-")+1)

This will return "GAD-C-005"

3. Let's repeat step #1 to find the location of the second dash in
what's remaining, "GAD-C-005":

instr(1, (mid([Identifier], instr(1,[Identifier], "-")+1) ),"-")

This results in a 4 which is the position of the "-" in "GAD-C-005".

4. You want to start at position 5, so add 1 to the expression in #3
for 5. Now, use the mid expression to find what results in position 5
and onward of the expression derived in #2:

mid((mid([Identifier],instr(1,[Identifier], "-")+1)),instr(1,
(mid([Identifier], instr(1,[Identifier], "-")+1) ),"-")+1)

This sort of thing can be confusing with all the parenthesis.

IS there a better way? Probably, but I've been doing this sort of thing
this way for years...
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "Whatcha doin?" - Ditto "TIM-MAY!!" - Me
Apr 7 '06 #3
This can probably be done in the query without code. Are the fields
sizes consistent? In other words, is region always two characters,
districts always three, etc? If so, you can create fields in the query
using the Left and Mid functions to grab those fields and then sort on
them. If the fields are not of fixed width but do use hyphens, as your
exmplae suggests, you would need to also use the InStr function to find
the positions of the hyphens.

HTH,
Barry

Apr 7 '06 #4
de**********@yahoo.com wrote in
news:11*********************@t31g2000cwb.googlegro ups.com:
I have a unique identifier consisting of regions, districts, a
road class value and then sequential numbers, in that order, eg:
GA-GAD-C-005 The thing is that the sequential values don't follow
onto each other as it does the sorting on the road class value
first (which could be A, I or C). So, you'll have the first three
parametres correct, but the number skips values, which appears in
a different "group" for that district. I need to thus sort the
roads by ignoring the road class and only start by sorting it on
the region, then district and then the sequential number. Do I
need to do this programmatically? I'd like to do this in SQL
however as most of my reports and forms run off the SQL
statements.


This is denormalized data. Subparts of the value stored in the
single field have individual meanings that are independent of the
other parts.

This should be broken down into 4 fields.

This will allow each field to be indexed (you'll want four indexes,
a unique compound one on the four fields together, and one
non-unique each on the other three fields), and will vastly increase
the speed of sorting and selecting on subparts of your value.

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
Apr 7 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
8
by: nidhog | last post by:
Hello guys, I made a script that extracts strings from a binary file. It works. My next problem is sorting those strings. Output is like: ---- snip ---- 200501221530
7
by: ryba | last post by:
Hello I'm sorry for mistakes - my English isn't very well. I've got the problem with sorting objects in ArrayList. If I put there only strings, Sort method works great, but it doesnt work when I...
3
by: Daniel Weinand | last post by:
hello ng, i have a problem and a imho an insufficient method of solution. strings should be sorted by specific text pattern and dispayed in groups. the strings are stored in a db and have the...
7
by: Pete Davis | last post by:
A different question this time. I have a DataGrid bound to a collection. Is there any way for me to allow sorting? The DataGrid.AllowSorting=true doesn't work, but that's probably because it can't...
3
by: SneakyElf | last post by:
i am very green with c++ so i get stuck on very simple things anyway, i need to write a program that would read data from file (containing names of tv shows and their networks) one line at a time...
16
by: skip | last post by:
The thread on sorting in Python 3 got me to thinking. How could I sort a list of complex numbers using key? As expected: Traceback (most recent call last): File "<stdin>", line 1, in...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.