Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

SQL: numeric sort of a varchar field

 
   Database Help (Home) -> Client RSS
Next:  Help setting up a view  
Author Message
eric.lecocq

External


Since: Jan 22, 2008
Posts: 2



(Msg. 1) Posted: Tue Jan 22, 2008 12:15 pm
Post subject: SQL: numeric sort of a varchar field
Archived from groups: microsoft>public>sqlserver>clients (more info?)

Hello Everybody,

my question is the following. how can sort numerically a sql query with
varchar field ?

thank you for your help.

ELE.

EG. sort a list of bus lines

1
14
2
23
3
4
44
5

must be

1
2
3
4
5
14
23
44

 >> Stay informed about: SQL: numeric sort of a varchar field 
Back to top
Login to vote
Paul Shapiro

External


Since: Dec 03, 2003
Posts: 109



(Msg. 2) Posted: Tue Jan 22, 2008 12:15 pm
Post subject: Re: numeric sort of a varchar field [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Use Order By Cast(columnName as int), as long as all values are legitimate
integers. But if so, why not make the column datatype int?

wrote in message

> Hello Everybody,
>
> my question is the following. how can sort numerically a sql query with
> varchar field ?

 >> Stay informed about: SQL: numeric sort of a varchar field 
Back to top
Login to vote
eric.lecocq

External


Since: Jan 22, 2008
Posts: 2



(Msg. 3) Posted: Tue Jan 22, 2008 1:07 pm
Post subject: Re: numeric sort of a varchar field [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

problem is that a bus line can contain character

eg. line "108" and "108A" and this must be sorted as 108 and then 108S


1
1A
2
34
3B
3BC
4


"Paul Shapiro" a écrit dans le message de
groupe de discussion : #Ou5e3OXIHA.2000@TK2MSFTNGP05.phx.gbl...
> Use Order By Cast(columnName as int), as long as all values are legitimate
> integers. But if so, why not make the column datatype int?
>
> wrote in message
>
>> Hello Everybody,
>>
>> my question is the following. how can sort numerically a sql query with
>> varchar field ?
>
 >> Stay informed about: SQL: numeric sort of a varchar field 
Back to top
Login to vote
Paul Shapiro

External


Since: Dec 03, 2003
Posts: 109



(Msg. 4) Posted: Wed Jan 23, 2008 12:22 am
Post subject: Re: numeric sort of a varchar field [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

That's harder. All I can think of is to write a function which finds the
first non-numeric character position. Sort by the numeric value of the
digits, and then by the remaining alpha portion.

wrote in message

> problem is that a bus line can contain character
>
> eg. line "108" and "108A" and this must be sorted as 108 and then 108S
>
>
> 1
> 1A
> 2
> 34
> 3B
> 3BC
> 4
>
>
> "Paul Shapiro" a écrit dans le message de
> groupe de discussion : #Ou5e3OXIHA.2000@TK2MSFTNGP05.phx.gbl...
>> Use Order By Cast(columnName as int), as long as all values are
>> legitimate integers. But if so, why not make the column datatype int?
>>
>> wrote in message
>>
>>> Hello Everybody,
>>>
>>> my question is the following. how can sort numerically a sql query with
>>> varchar field ?
>>
 >> Stay informed about: SQL: numeric sort of a varchar field 
Back to top
Login to vote
Mike C#

External


Since: Jan 12, 2008
Posts: 483



(Msg. 5) Posted: Wed Jan 23, 2008 7:26 pm
Post subject: Re: numeric sort of a varchar field [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Your best bet is to separate out the numeric part from the character part
into two separate columns. Here's a simple sample query that you could use
as a basis for that. It's not going to be extremely efficient - I'd
recommend splitting them up one time permanently if I were you:

CREATE TABLE #t (n VARCHAR(100))
INSERT INTO #t (n)
SELECT '1'
UNION SELECT '1A'
UNION SELECT '2'
UNION SELECT '34'
UNION SELECT '3B'
UNION SELECT '3BC'
UNION SELECT '4'
GO

WITH Numbers (num)
AS
(
SELECT 1
UNION ALL
SELECT num + 1
FROM Numbers
WHERE num < 100
)
SELECT CAST(SUBSTRING(#t.n, 1, MIN(num) - 1) AS INTEGER) AS NumPart,
SUBSTRING(#t.n, MIN(num), 255) AS AlphaPart
FROM Numbers
CROSS APPLY #t
WHERE SUBSTRING(#t.n + 'Z', num, 1) BETWEEN 'A' AND 'Z'
GROUP BY #t.n;


"Paul Shapiro" wrote in message

> That's harder. All I can think of is to write a function which finds the
> first non-numeric character position. Sort by the numeric value of the
> digits, and then by the remaining alpha portion.
>
> wrote in message
>
>> problem is that a bus line can contain character
>>
>> eg. line "108" and "108A" and this must be sorted as 108 and then 108S
>>
>>
>> 1
>> 1A
>> 2
>> 34
>> 3B
>> 3BC
>> 4
>>
>>
>> "Paul Shapiro" a écrit dans le message de
>> groupe de discussion : #Ou5e3OXIHA.2000@TK2MSFTNGP05.phx.gbl...
>>> Use Order By Cast(columnName as int), as long as all values are
>>> legitimate integers. But if so, why not make the column datatype int?
>>>
>>> wrote in message
>>>
>>>> Hello Everybody,
>>>>
>>>> my question is the following. how can sort numerically a sql query with
>>>> varchar field ?
>>>
>
 >> Stay informed about: SQL: numeric sort of a varchar field 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
OLEDB: Prepared cmd with NULL input param for NOT NULL INT.. - Provider: SQLOLEDB Version: SQL Server 2000 8.00.760 Script for creating table: CREATE TABLE [dbo].[ADRVERTRETER] ( <font color=purple> ; [ROWID] [timestamp] NULL ,</font> <font color=purple> ; [ROWVERTRETER] [int] NO...

[MS Design Tools] - Class not registered.&quot; - On a Windows 2000 pro workstation I am using SQL 2000 enterprise manager I am trying to return all rows, I get the error message "An unexpected error happened during this operation. [MS Design Tools] - Class not registered." I have reinstall...

Need help choosing front end for SQL Server - I've been working on an Access 97 database that's pretty much reached it's limit in terms of performance and reliability. Although it supports relatively few users (5-10 concurrent) it contains a lot of data (around 30 tables, some with several million..

SQL server tables read-only to ADP - I have successfully migrated an Access 2003 database to SQL server - at least all the tables and queries that resolve into views migrated successfully. I also sucessfully migrated all my forms and code into an ADP project. Everything works with one..

Opening table in SQL Server 2005? - This ought to be easy. But I can't figure out how to do it. Using Enterprise Manager in SQL Server 2000, I point to a table, select it, right click and choose Open Table-> Return all rows. Now I have the table open and I can edit to my heart's..
   Database Help (Home) -> Client All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]