Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi folks,

I am in critical position any one can help me from this condition . Actual my problem is in my table there is a nvarchar type data which having data like

1
0.2a
1a
a.2
z
a
001
08
008.003


I need output as:
0.2a
1
001
1a
08
8.003
a
a.2
z

Thanks in advance
Seshu
Posted
Updated 10-May-11 23:47pm
v2
Comments
mhwasim 11-May-11 6:01am    
Is there any other relative field associated with it? The order you want it seems to be having some custom criteria. Can you share the logic of this sorting list?
hiseshu 11-May-11 6:12am    
This column is nvarchar type and having different types of data as i mentioned above . There is no other field related to it..
mhwasim 11-May-11 9:25am    
I have added a solution. Kindly check it

SQL
SELECT id FROM sortaln ORDER BY
CASE WHEN ISNUMERIC(id) = 1 THEN RIGHT(REPLICATE('0',51) + id, 50)
     WHEN ISNUMERIC(id) = 0 THEN LEFT(id + REPLICATE('',51), 50)
     ELSE id
END

try this i hope it helps you
 
Share this answer
 
v2
Comments
hiseshu 11-May-11 5:58am    
hi

it wont works .. is there any alternate way..

Seshu
Hi there,

Would it make sense if you split the list of data into two? One list containing only numbers, the other containing the rest of it. Sort these 2 lists separately, and merge them (append the other list to the end of the numeric list)

I suppose this isn't hard to do, but you will need to write a stored procedure with a temporary table or two.

Hope this helps :) Regards
 
Share this answer
 
Please check the following SQL code. I hope it will now resolve ur problem....

CREATE TABLe #T -- original
(
T VARCHAR(10)
)

CREATE TABLe #TSort --sorted
(
ID INT IDENTITY,
T VARCHAR(10)
)

INSERT INTO #T
SELECT '0.2a' UNION ALL
SELECT '1' UNION ALL
SELECT '001' UNION ALL
SELECT '1a' UNION ALL
SELECT '08' UNION ALL
SELECT '8.003' UNION ALL
SELECT 'a' UNION ALL
SELECT 'a.2' UNION ALL
SELECT 'z' UNION ALL
SELECT '10' UNION ALL
SELECT '11' UNION ALL
SELECT '2' UNION ALL
SELECT '3' UNION ALL
SELECT '31' UNION ALL
SELECT '4' UNION ALL
SELECT '46' UNION ALL
SELECT '21'


INSERT INTO #TSort
SELECT T
FROM #T
WHERE (ISNUMERIC(LEFT(T, LEN(T) - 1)) = 1 AND ISNUMERIC(T) = 0) OR ISNUMERIC(T) = 1
ORDER BY CASE
WHEN (ISNUMERIC(LEFT(T, LEN(T) - 1)) = 1 AND ISNUMERIC(T) = 0) THEN CONVERT(FLOAT,LEFT(T, LEN(T) - 1))
WHEN ISNUMERIC(T) = 1 THEN CONVERT(FLOAT,T) END

INSERT INTO #TSort
SELECT T FROM #T WHERE ISNUMERIC(T) = 0 AND ISNUMERIC(LEFT(T, LEN(T) - 1)) = 0
ORDER BY T

SELECT T FROM #TSort ORDER BY ID

DROP TABLE #T
DROP TABLE #TSort

Thanks
 
Share this answer
 
v3
Comments
hiseshu 12-May-11 1:36am    
hi
sorry it wont works i am getting output as 1 1 1 10 11 12 19 2 21 22 23 3 31 32 4 like that

Thanks

Seshu
mhwasim 12-May-11 5:48am    
This has been fixed..pls check..Thanks
hiseshu 12-May-11 6:08am    
Thanks i got it...
Your query should be :
Select * from table_name order by column_name

ascending is by default
for descending :
Select * from table_name order by column_name desc
 
Share this answer
 
v2
Comments
hiseshu 11-May-11 5:58am    
hi already i tried in that way it doesn't works

Seshu

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900