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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Sort varchar field like int
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

9 Posts

Posted - 09/17/2008 :  07:22:45  Show Profile  Reply with Quote
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,

Premature Yak Congratulator

22864 Posts

Posted - 09/17/2008 :  08:33:04  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote


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

Flowing Fount of Yak Knowledge

6065 Posts

Posted - 09/17/2008 :  08:45:16  Show Profile  Reply with Quote
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

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

Be One with the Optimizer
Go to Top of Page

Posting Yak Master

112 Posts

Posted - 04/01/2013 :  22:06:54  Show Profile  Reply with Quote
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?

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

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 04/02/2013 :  01:32:10  Show Profile  Reply with Quote
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
Go to Top of Page

Posting Yak Master

112 Posts

Posted - 04/02/2013 :  01:41:17  Show Profile  Reply with Quote
Go to Top of Page

Patron Saint of Lost Yaks

30421 Posts

Posted - 04/03/2013 :  04:02:39  Show Profile  Visit SwePeso's Homepage  Reply with Quote
SELECT		[Date],
		RIGHT('0' + DATENAME(wk, [date]), 2) + '/' + DATENAME(yy, [date]) AS WeekYear
join		Agents on engineer=agent_login_id
order by	WeekYear

N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000