Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Sort varchar field like int

Author  Topic 

stevecurrey
Starting Member

9 Posts

Posted - 2008-09-17 : 07:22:45
Hi all - bit of a nightmare this one...

A table in a database has a varchar(50) field called episodeno (episode number). The values in here are usually numbers but can also be something like '1 of 4' or 'Pilot'.

There is a query which searches for episodes by progtitle

SELECT * FROM progs WHERE progtitle LIKE '%@searchstring%' ORDER BY progtitle ASC, episodeno ASC


This will then sort episode numbers as 1,10,2,3,4 etc.

Can you see any way I can get round this? I can't change the field type.

Thanks in advance,
Stephen.

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-09-17 : 08:33:04
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/07/21/ordering-interger-values-stored-in-varchar-column.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-09-17 : 08:45:16
Perfect example of why you should use the correct datatypes.
This solution may work but it will likely be VERY slow.

for the cases where they are just numbers you can righ justify with something like:
order by ...
,replicate(' ', 50-len(episodeno)) + episodeno asc

If there are not too many types of exceptions for non-numerics (like the two you mentioned) you may be able to use CASE statement. One WHEN per exception type:

(this code is untested)

order by ...
,case -1
when episodeno not like '%[^0-9]%' then replicate(' ', 50-len(episodeno)) + episodeno
when episodeno like '%of%' then left(episodeno, charindex('of', episodeno)-1)
when episodeno = 'Pilot' then 0
end

for this solution you would also need to add the logic to "right justify" the other CASEs.


Be One with the Optimizer
TG
Go to Top of Page

2revup
Posting Yak Master

112 Posts

Posted - 2013-04-01 : 22:06:54
Sorry to bring up an old topic have the same issue but I am having trouble with this I have read your blog madhivanan but no joy

I am doing a datepart and joining them,so in doing this I have created them as varchar to get the '/' in there, can anyone help with this one?

SELECT [Date]
,[Engineer]
,[Case]
,Supervisor_name
,Location
,CONVERT(nvarchar(2),DATEPART(wk,[date])) + '/' + CONVERT(nvarchar(4),DATEPART(yy,[date])) as WeekYear
FROM GNC
join Agents on engineer=agent_login_id
order by WeekYear+0
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-02 : 01:32:10
if you want sorting them based on their numeric value, you cant have them as varchar.
In my opinion best thing would be to bring them as DATENAME(yy,[date]) + DATENAME(wk,[date]) and then do formatting at front end to add /

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

2revup
Posting Yak Master

112 Posts

Posted - 2013-04-02 : 01:41:17
Cheers
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2013-04-03 : 04:02:39
[code]SELECT [Date],
[Engineer],
[Case],
Supervisor_name,
Location,
RIGHT('0' + DATENAME(wk, [date]), 2) + '/' + DATENAME(yy, [date]) AS WeekYear
FROM GNC
join Agents on engineer=agent_login_id
order by WeekYear[/code]


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -