473,587 Members | 2,230 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
programmaticall y? 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 1809
-----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:::mgf0 0 <at> earthlink <decimal-point> net
Oakland, CA (USA)

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

iQA/AwUBRDaaiIechKq OuFEgEQJYUQCgld N6AgiVWmNJhEavu BA3uyFPR0IAnRoL
y8PT29cwcxet2Xx VnEkLgwd9
=p/vx
-----END PGP SIGNATURE-----
de**********@ya hoo.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
programmaticall y? 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**********@ya hoo.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)),inst r(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)),inst r(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**********@ya hoo.com wrote in
news:11******** *************@t 31g2000cwb.goog legroups.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 programmaticall y? 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
3253
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)) CType(local4, Short) = CType(src, Short)
22
4132
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) { return -1; } else
8
2254
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
1812
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 put there objects (even if ToString method is override in object class). I think that i have to override a comparator in my object class, but even...
3
1920
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 following layout: 1.0.0.0 1.1.0.0 1.1.1.0 1.1.2.0
7
4219
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 assume the data types and thus can't sort them. I thought about implementing IComparable, but I don't see how that would work since it doesn't...
3
2182
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 ( ; separates tv show and network) sort the strings according to the network and according to the show. and so: //to open file - ifstream...
16
9845
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 <module> TypeError: no ordering relation is defined for complex numbers
5
4922
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 there are, it crashes. There are currently 6 columns, and I only want 4. How do I remove the last two (discount and date)? Here is a link:...
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8339
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.