473,396 Members | 1,864 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,396 software developers and data experts.

How to sort a text field that contains numbers only?

Hi..

I need to query a TEXT field in an ODBC table (Oracle) that contained numbers.

1) How can I sort those numbers in numeric order? Currently, number 1 ~ 11 is sorted in this order.. 1, 10, 11, 2, 3... 9.

2) If #1 is not possible, I would like to export the query result to a table. How do I then change the data type from TEXT to NUMBER using VB codes? Currently, I am changing this manually which is not user-friendly.

Cheers!
Clarence
Oct 23 '07 #1
8 5227
JamieHowarth0
533 Expert 512MB
Hi Clarence,

1) is not possible because the database recognises the field as type TEXT so it sorts it alphabetically (by ASCII codes like a dictionary), not numerically - so a bit like sorting a, ab, adbc, b, ba, be, etc.

2) is possible using a very swift, automatic and simple bit of ASP:

Expand|Select|Wrap|Line Numbers
  1. Set oConn = Server.CreateObject("ADODB.Connection")
  2. sConn = "Driver={Microsoft ODBC Driver for Oracle};DBQ=MapToYourDB"
  3. oConn.Open sConn
  4.  
  5. 'First: you want to add a new field to your table with type INTEGER
  6. Set rs = Server.CreateObject("ADODB.Recordset")
  7. rsSQL = "ALTER TABLE yourtable ADD COLUMN ('new_integer_column', int);"
  8. rs.Open oConn, rsSQL
  9. rs.Close
  10. 'Done
  11.  
  12. 'Second: you want to retrieve your text value and store it as integer
  13. rsSQL = "SELECT * FROM yourtable;"
  14. rs.Open oConn, rsSQL
  15.  
  16. 'Set value of new column to be the INTEGER value of the old text column (VB has the built-in function CInt - short for Convert to Integer type)
  17. For I = 1 To rs.RecordCount
  18. rs("new_integer_column") = CInt(rs("old_text_column")
  19. rs.Update
  20. rs.MoveNext
  21. Next
  22. rs.Close
  23. 'Done
  24.  
  25. 'Third: you want to (ideally) delete your text column as it's now redundant.
  26.  
  27. rsSQL = "ALTER TABLE yourtable DROP COLUMN ('old_text_column');"
  28. rs.Open oConn, rsSQL
  29. rs.Close
  30. 'Done!
  31. %>
  32.  
Obviously this needs some tweaking to make it work for your own circumstances and server setup but it should be sufficient.

Hope it helps.

medicineworker

P.S. Should really be in the Oracle or VB/ASP forum :-)
Oct 23 '07 #2
Hi medicineworker,

Thanks for your prompt advise.

I may not have made myself clear in my questions.

I am developing a MS Access database that requires me to Link Oracle tables via ODBC.

Thus, if #1 is not feasible, I would then need to export the Oracle table to a new table within MS Access. How do I then change the data type of this particular text field to a number field?

Cheers!
Clarence


Hi Clarence,

1) is not possible because the database recognises the field as type TEXT so it sorts it alphabetically (by ASCII codes like a dictionary), not numerically - so a bit like sorting a, ab, adbc, b, ba, be, etc.

2) is possible using a very swift, automatic and simple bit of ASP:

Expand|Select|Wrap|Line Numbers
  1. Set oConn = Server.CreateObject("ADODB.Connection")
  2. sConn = "Driver={Microsoft ODBC Driver for Oracle};DBQ=MapToYourDB"
  3. oConn.Open sConn
  4.  
  5. 'First: you want to add a new field to your table with type INTEGER
  6. Set rs = Server.CreateObject("ADODB.Recordset")
  7. rsSQL = "ALTER TABLE yourtable ADD COLUMN ('new_integer_column', int);"
  8. rs.Open oConn, rsSQL
  9. rs.Close
  10. 'Done
  11.  
  12. 'Second: you want to retrieve your text value and store it as integer
  13. rsSQL = "SELECT * FROM yourtable;"
  14. rs.Open oConn, rsSQL
  15.  
  16. 'Set value of new column to be the INTEGER value of the old text column (VB has the built-in function CInt - short for Convert to Integer type)
  17. For I = 1 To rs.RecordCount
  18. rs("new_integer_column") = CInt(rs("old_text_column")
  19. rs.Update
  20. rs.MoveNext
  21. Next
  22. rs.Close
  23. 'Done
  24.  
  25. 'Third: you want to (ideally) delete your text column as it's now redundant.
  26.  
  27. rsSQL = "ALTER TABLE yourtable DROP COLUMN ('old_text_column');"
  28. rs.Open oConn, rsSQL
  29. rs.Close
  30. 'Done!
  31. %>
  32.  
Obviously this needs some tweaking to make it work for your own circumstances and server setup but it should be sufficient.

Hope it helps.

medicineworker

P.S. Should really be in the Oracle or VB/ASP forum :-)
Oct 23 '07 #3
JamieHowarth0
533 Expert 512MB
Hi Clarence,

Aha! A much easier solution is in order!

If your linked table is already set up, create a query in Access, with the following SQL code:

Expand|Select|Wrap|Line Numbers
  1. SELECT CInt(old_text_field) AS new_int_field, field1, field2.... etc. etc. FROM linked_oracle_table ORDER BY new_int_field;
When the query runs, it will convert all the text values in the linked table into integer values (in a temporary column called new_int_field) and automatically sort upon that field (which will be numeric, which gets the order you require).

The one great thing about SQL (especially Access SQL) is that it contains a number of VB-esque functions for data conversion, which (as shown in this case) can be incredibly handy!

Should sort your problem out quite nicely!

If you do need to import the table from Oracle into Access (thus removing the linked table bit), you create the table but instead of the text field being specified as Text in Access' Table Designer, specify it as Number, then copy and paste your existing records in.

All the best,

medicineworker
Oct 23 '07 #4
Hi medicineworker,

Thanks again.

When I run the suggested SQL code (in Query), i was prompted to enter parameter value for new_int_field.

My sql code was modified as follow.

SELECT CInt([MYFIELD]) AS new_int_field
FROM MYODBC_TABLE
ORDER BY new_int_field;

Kindly advise what could have gone wrong.

Cheers!
Clarence

Hi Clarence,

Aha! A much easier solution is in order!

If your linked table is already set up, create a query in Access, with the following SQL code:

Expand|Select|Wrap|Line Numbers
  1. SELECT CInt(old_text_field) AS new_int_field, field1, field2.... etc. etc. FROM linked_oracle_table ORDER BY new_int_field;
When the query runs, it will convert all the text values in the linked table into integer values (in a temporary column called new_int_field) and automatically sort upon that field (which will be numeric, which gets the order you require).

The one great thing about SQL (especially Access SQL) is that it contains a number of VB-esque functions for data conversion, which (as shown in this case) can be incredibly handy!

Should sort your problem out quite nicely!

If you do need to import the table from Oracle into Access (thus removing the linked table bit), you create the table but instead of the text field being specified as Text in Access' Table Designer, specify it as Number, then copy and paste your existing records in.

All the best,

medicineworker
Oct 23 '07 #5
JamieHowarth0
533 Expert 512MB
Hi Clarence,

Try:
Expand|Select|Wrap|Line Numbers
  1.  SELECT (CInt([MYFIELD]) AS [new_int_field]) FROM MYODBC_TABLE ORDER BY new_int_field;
Notice addition of square brackets around new_int_field and normal brackets around the field list.

Any further problems then let me know.

Best regards,
medicineworker
Oct 24 '07 #6
Hi medicineworker,

I have tried the revised code.

There seems to be some syntax error (missing operator) in the query expression. I tried to troubleshoot it but to no avail.

Cheers!
Clarence


Hi Clarence,

Try:
Expand|Select|Wrap|Line Numbers
  1.  SELECT (CInt([MYFIELD]) AS [new_int_field]) FROM MYODBC_TABLE ORDER BY new_int_field;
Notice addition of square brackets around new_int_field and normal brackets around the field list.

Any further problems then let me know.

Best regards,
medicineworker
Oct 24 '07 #7
JamieHowarth0
533 Expert 512MB
Hi Clarence,

I just tried it myself, for some reason Access is being a bit weird about handling it but I finally got a piece of working code:
Expand|Select|Wrap|Line Numbers
  1. SELECT CInt(DummyID) AS Expr1 FROM tbl_Test ORDER BY CInt(DummyID);
For some reason Access accepts the Expr1 (temp column name) but then refuses to sort on it...

Anyway, it works, so should work for your code.

Best regards,

medicineworker
Oct 24 '07 #8
Hi medicineworker,

Great! It's now running!

Thanks for your great help!

Cheers!
Clarence


Hi Clarence,

I just tried it myself, for some reason Access is being a bit weird about handling it but I finally got a piece of working code:
Expand|Select|Wrap|Line Numbers
  1. SELECT CInt(DummyID) AS Expr1 FROM tbl_Test ORDER BY CInt(DummyID);
For some reason Access accepts the Expr1 (temp column name) but then refuses to sort on it...

Anyway, it works, so should work for your code.

Best regards,

medicineworker
Oct 25 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Andrew | last post by:
Hi, I have a form set up which collects details and then when submitted it creates a new record in a table called rbh_vacancies Most of the time it adds the records perfectly, but there seems...
2
by: Paulo Rodrigues | last post by:
Hi I would like some help about the following : I have a text field and I don't want it contains any numbers. How can I limit this situation ? So far, I couldn't find literature exacly...
3
by: steven | last post by:
Help, probably a simple mistake. Trying to take a text field containing numbers in a query for a combobox. The number is 9 characters and I wish to display only the last 4. Using help and the net...
11
by: Woody Splawn | last post by:
Are there any problems with saving values to a database from a text field that has an apostrophy in it? I am using SQL Server 2000. My notes are not clear on this, but I seem to remember that if...
4
by: Tony WONG | last post by:
i have a number of forms with fax numbers to come up into arrays and then combine to string. after that i design the flow 1. break the string to array now the string looks like this...
8
by: Alfa & Omega | last post by:
Is there any way in .NEt to make textbox which will automaticly accept only numbers? Or do I have to check on textChange is inserted character number or not?
1
by: Redbeard | last post by:
I have a text field that I used to store the unique number of each record. I wish to sort this number but since it is in a text field it will not sort numerically. I have the number in a text field...
1
by: madhu sudhanan | last post by:
hey, i need to validate a text field in Visual Basic with alphanumeric of 3 alphabets in the left and three numbers in the right and it should not allow any other special characters,space,backspace...
1
doma23
by: doma23 | last post by:
I have a field in a query that contains numbers and text (text field): C/R: IIf(<=0;"n/m";/) I need to export this query to Excel in a number format, so I can easily manipulate the data later....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.