SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

stevecurrey
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,
Stephen.

madhivanan
Premature Yak Congratulator

India
22761 Posts

Posted - 09/17/2008 :  08:33:04  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
6062 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
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 - 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?

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

India
52323 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
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

2revup
Posting Yak Master

112 Posts

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

SwePeso
Patron Saint of Lost Yaks

Sweden
30265 Posts

Posted - 04/03/2013 :  04:02:39  Show Profile  Visit SwePeso's Homepage  Reply with Quote
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



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.06 seconds. Powered By: Snitz Forums 2000