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

SQL: numeric sort of a varchar field

 
   Database Forums (Home) -> Client RSS
Next:  Alpha value in float field  
Author Message
eric.lecocq

External


Since: Jan 22, 2008
Posts: 2



(Msg. 1) Posted: Tue Jan 22, 2008 7:01 am
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: 42



(Msg. 2) Posted: Tue Jan 22, 2008 7:01 am
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 11:02 am
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: 42



(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: Jul 27, 2006
Posts: 212



(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:
How to get rid of dbo in field names? - Hello, I just converted a website database from MS Access to SQL 2000, and many of the pages are not working because SQL now wants fields to be ref'd as "dbo.<<fieldname>>"... I've fixed this before, but cannot remember how... I...

dts memo field - Hello, I have a Foxpro memo field, I was able to import the MEMO field using the Foxpro ODBC driver, however, not all information from the memo fields are imported. For example, a record has 11 lines of information, it would only import part of it...

Urgent help ! DLU field - I have a table that contains couple of fields but one of field is DLU (Date Last updated) I want when any records changes the DLU field got updated to current time stamp, I created trigger for that but I think trigger is not the solution becuase when....

problem trying to save a date field - Hi there, right now i'm working with a VB6 project that connects to a SQLSERVER 2000 using a System DSN. I discovered a problem that, i hope you can help me. The problem is that, when i try to save a record with a date field - the date saved in..

Export the Data of a field of 5000 characters length - I have a table like that: Create table myTable ( Name varchar, Description Text ) The description column contains more than 300 characters. The sql limit is to show only 30,35 words. Is there any tool available which can export around 5000 character...
   Database Forums (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 ]