| Author |
Topic |
|
ronmc
Starting Member
12 Posts |
Posted - 2005-07-28 : 15:12:33
|
| Can any tell me how to do a sort on a field where the datatype is varchar, but the data in the table is alphanumeric?I looked at BOL, and it gave a really nice dissertation on collation, etc., but I'm hoping that this will be a little easier that I'm making out to be.Here is my code so far:select adjcode, reason from adjcodes order by adjcodeResults:1 Bad Stuff10 Really Bad Stuff2 Out of order here |
|
|
vr8ce
Starting Member
23 Posts |
Posted - 2005-07-28 : 16:08:23
|
quote: Originally posted by ronmc Can any tell me how to do a sort on a field where the datatype is varchar, but the data in the table is alphanumeric?
See CAST. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-07-29 : 01:29:21
|
| If adjcode has only numbers thenselect adjcode, reason from adjcodes order by adjcode * 1Otherwiseselect adjcode, reason from adjcodes order by len(adjcode),adjcodeBut it is better to use the proper datatypeMadhivananFailing to plan is Planning to fail |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2005-07-29 : 04:14:02
|
| "order by len(adjcode),adjcode"I like that ... maybe lends itself toorder by CASE WHEN IsNumeric(adjcode) THEN len(adjcode) ELSE 99999 END, adjcodeso all the non numerics are at one end - allowing for the vagaries of IsNumeric() of course ...Kristen |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-07-29 : 04:57:19
|
>>order by CASE WHEN IsNumeric(adjcode) THEN len(adjcode) ELSE 99999 END, adjcodeThats good point except that IsNumeric(adjcode) should be IsNumeric(adjcode)=1 MadhivananFailing to plan is Planning to fail |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2005-07-29 : 05:34:40
|
| Catches me out every time - I'm used to languages where TRUE is useable logic result :-(Kristen |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-07-29 : 05:51:18
|
No problem. Thats nature MadhivananFailing to plan is Planning to fail |
 |
|
|
ronmc
Starting Member
12 Posts |
Posted - 2005-07-29 : 10:16:17
|
| Thanks everyone for the help! This code works great:select * from adjcodesorder by CASE WHEN IsNumeric(adjcode)=1 THEN len(adjcode) ELSE 99999 END, adjcodeI really appreciate all your help |
 |
|
|
Makans
Starting Member
2 Posts |
Posted - 2005-08-17 : 09:16:46
|
quote: Originally posted by madhivanan No problem. Thats nature MadhivananFailing to plan is Planning to fail
Hi can You help me? I have same problem: I want to sort column, which type is varchar and data is numbers and also character. I work with Interbase and C++ Builder4.Hope to get your ansvers! |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-08-17 : 09:42:07
|
| WellSee if this worksSelect columns from yourTable order by len(varcharField), varcharfieldMadhivananFailing to plan is Planning to fail |
 |
|
|
Makans
Starting Member
2 Posts |
Posted - 2005-08-17 : 09:47:44
|
quote: Originally posted by madhivanan WellSee if this worksSelect columns from yourTable order by len(varcharField), varcharfieldMadhivananFailing to plan is Planning to fail
No this do not work! |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-08-17 : 10:11:00
|
| Can you post some sample data with the result you want?MadhivananFailing to plan is Planning to fail |
 |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2005-08-17 : 11:49:46
|
| "I work with Interbase and C++ Builder4"...this is a SQL Server site. Try www.dbforums.com |
 |
|
|
|