| Author |
Topic |
|
oko_sakti
Starting Member
6 Posts |
Posted - 2005-12-30 : 02:02:47
|
| Dear all,I have a varchar field which have data:ID_trans---------T1T10T11T2T3everytime i use select statement with order by, the result is aboveDoes somebody know..? HOw to make the result like belowID_trans---------T1T2T3T10T11Sorry i'm not good in english...Thanks |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-12-30 : 02:04:44
|
| Select columns from yourTable Order by len(ID_trans),ID_transMadhivananFailing to plan is Planning to fail |
 |
|
|
sachinsamuel
Constraint Violating Yak Guru
383 Posts |
Posted - 2005-12-30 : 02:13:45
|
| Or else you can even try thisCreate table TEST1( A varchar(20))Insert into TEST1 values ('T1')Insert into TEST1 values ('T3')Insert into TEST1 values ('T11')Insert into TEST1 values ('T2')Insert into TEST1 values ('T10')Select (left(A,1)+ convert(varchar,Substring(A,2,len(A)))) from TEST1 Order by convert(int,Substring(A,2,len(A)))RegardsSachinDon't sit back because of failure. It will come back to check if you still available. -- Binu |
 |
|
|
oko_sakti
Starting Member
6 Posts |
Posted - 2005-12-30 : 02:21:04
|
| That's workThank you |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-12-30 : 02:24:58
|
>>Select (left(A,1)+ convert(varchar,Substring(A,2,len(A)))) from TEST1 Order by convert(int,Substring(A,2,len(A)))No need to use Convert. That will work only if the first charater is Alphabet MadhivananFailing to plan is Planning to fail |
 |
|
|
oko_sakti
Starting Member
6 Posts |
Posted - 2005-12-30 : 02:36:11
|
| That's workThank you |
 |
|
|
oko_sakti
Starting Member
6 Posts |
Posted - 2005-12-30 : 02:47:35
|
| What about thisID_trans---------S1S2S100S10ST1T1T10T11T100T2T3everytime i use select statement with order by, the result is aboveDoes somebody know..? HOw to make the result like belowID_trans---------S1S2S10S100ST1T1T2T3T10T11Thanks |
 |
|
|
sachinsamuel
Constraint Violating Yak Guru
383 Posts |
Posted - 2005-12-30 : 03:59:12
|
| There is a function in VB called Val(). This returns only number, if string is passed to it.I have something equivalent to it below.Create FUNCTION fnValOnly(@x varchar(8000))RETURNS intAS BEGIN DECLARE @n int, @MAX_n int, @z varchar(8000) DECLARE @y table(pos int, value char(1)) SELECT @MAX_n = LEN(@x), @n = 1, @z = '' WHILE @N<=@MAX_n BEGIN INSERT INTO @y(pos,value) SELECT @n, SUBSTRING(@x,@n,1) SELECT @n = @n + 1 END select @z =@z +value from @y where isnumeric (value)=1 RETURN @z ENDGOThis is a scalar function and returns int. You can use this function in your query and get the integer separated from character(S and ST), then order by only on number separated and then concatinate.regardsSachinDon't sit back because of failure. It will come back to check if you still available. -- Binu |
 |
|
|
Frank Kalis
Constraint Violating Yak Guru
413 Posts |
Posted - 2005-12-30 : 04:20:13
|
| [code]CREATE TABLE #t( c1 VARCHAR(5))INSERT INTO #t SELECT 'S1'UNION ALL SELECT 'T3'UNION ALL SELECT 'S2'UNION ALL SELECT 'T2'UNION ALL SELECT 'S10'UNION ALL SELECT 'S100'UNION ALL SELECT 'ST1'UNION ALL SELECT 'T1'UNION ALL SELECT 'T10'UNION ALL SELECT 'T11'SELECT * FROM #t ORDER BY LEFT(c1, PATINDEX('%[0-9]%', c1) - 1), CAST(SUBSTRING(c1, PATINDEX('%[0-9]%', c1), LEN(c1)-PATINDEX('%[0-9]%',c1)) AS INT) DROP TABLE #tc1 ----- S1S2S10S100ST1T1T2T3T10T11(10 row(s) affected)[/code]Consider doing this at the client. PHP, for example, has a handy function "natsort", that will do the trick.--Frank KalisMicrosoft SQL Server MVPhttp://www.insidesql.deHeute schon gebloggt? http://www.insidesql.de/blogs |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-12-30 : 04:54:48
|
>>Consider doing this at the client. PHP, for example, has a handy function "natsort", that will do the trick.Frank, you seem good in Clent Programming also MadhivananFailing to plan is Planning to fail |
 |
|
|
Frank Kalis
Constraint Violating Yak Guru
413 Posts |
|
|
|