Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL Sorting :

Datatype varchar


Should sort by
1.aaaa
5.xx
11.bbbbbb
12
15. how can i get this sorting order

Wrong
1.aaaa
11.bbbbbb
12
15.
5.xx
Posted
Updated 6-Dec-13 0:19am
v2
Comments
Sebastian T Xavier 6-Dec-13 6:49am    
don't repost

create FUNCTION [dbo].[sortingfunction]
(
@column varchar(max)
)
RETURNS varchar(max)
AS
BEGIN
declare @num varchar(50)
declare @space varchar(50)
declare @index int = 1

set @num = LEFT(SUBSTRING(@column, PATINDEX('%[0-9.-]%', @ column), 8000), PATINDEX('%[^0-9.-]%', SUBSTRING(@column, PATINDEX('%[0-9.-]%', @ column), 8000) + 'X')-1)
set @space = replicate(' ', 20 - len(@num))

return replace(@column, @num, @space + @num)
END


----------------------------------------------------------------------
select from sampletable order by dbo.sortingfunction(key)
 
Share this answer
 
Comments
patil.nitin8358 6-Dec-13 7:46am    
hello BrishPandey,

Thanks for the help , it solve my problem. good

Regards ,

nitin
patil.nitin8358 8-Dec-13 5:00am    
hello BrishPandey,

function create query work well on local sql server 2008 R2 while on remote sql server 2005

it saying that

Msg 139, Level 15, State 1, Procedure sortingfunction, Line 0
Cannot assign a default value to a local variable.


regards,

nitin
patil.nitin8358 12-Dec-13 6:19am    
hello BrishPandey,

above function is not working well

1
13.abc
2.1 abc
2.2
311.test

which is wrong , even though 13 is greater than 2.1 & 2.2 still displaying 13 ahead of 2.1 & 2.2

what changes should be done in function?

please help


regards,

nitin
You may refer to Sort string as number in sql server[^].

If the field only contains numbers, why using a string datatype instead of an integer one?
 
Share this answer
 
Hi Patil,

I hope you column data type is varchar, that is the reason sorting is done like what you have mentioned.
Here[^] you can find solution.

Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
Comments
patil.nitin8358 6-Dec-13 6:23am    
hello master,

SELECT *
FROM table
ORDER BY CAST(SUBSTRING(column,LOCATE('.',column)+1) AS SIGNED)

here ,when i use this it gives error to me for locate function , i think it is for mysql


could u please suggest me any other solution for

Should sort by
1.aaaa
5.xx
11.bbbbbb
12
15. how can i get this sorting order

Wrong
1.aaaa
11.bbbbbb
12
15.
5.xx




Regrads,

nitin
hi ,

Try this

SQL
SELECT * FROM MyTable
ORDER BY CAST(amount AS Numeric(10,0)) DESC
 
Share this answer
 
Comments
patil.nitin8358 6-Dec-13 6:32am    
Hello BrishPandey

column is varchar not an numeric

when i run ur suggester query it gives an error

Error converting data type varchar to numeric.

Thanks
how about this..

SQL
SELECT * FROM Table1
Order by
CASE  WHEN CHARINDEX('.',COL1) > 0
    THEN CAST(SUBSTRING(Col1,1, CHARINDEX('.',COL1)-1)  AS int)
    ELSE COL1 END


It is in sql server.
 
Share this answer
 
Comments
patil.nitin8358 6-Dec-13 7:04am    
Hello Ashish_Agrawal,
it saying

Conversion failed when converting the varchar value '2MyDream' to data type int.

thanks
Ashish_Agrawal 6-Dec-13 7:54am    
This query works with your example. Can you post me the column value where you are getting conversion failed issue?
patil.nitin8358 6-Dec-13 8:11am    
Hello Ashish_Agrawal,

your query also solve my problem iff my column contain value of format
11.asd
2.s
3.

if i have column with value 2MyDream then it gives error

Conversion failed when converting the varchar value '2MyDream' to data type int.


Thanks for the help

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