PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > comp.db.ms-sqlserver > How To Convert String to Numeric for Sorting?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How To Convert String to Numeric for Sorting?

Réponse
 
LinkBack Outils de la discussion
Vieux 24/03/2008, 02h04   #1
-cicada-
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How To Convert String to Numeric for Sorting?

I have a table of auction lots, where the lot number (Lot_No) is a
text (nvarchar) field. The reason that an nvarchar field has been
selected, is although most lot numbers are numeric, occasionally
auctioneers add in extra lots consisting of a numeric lot number,
followed by a letter, so an auction may have lots 101, 101, 101A, 101B
102, 103 etc. Being a text field, by default the Lot_No field sorts in
ascending order like this:
1
11
12
13
13A
.....
2
20
21

I want to sort field Lot_No into true ascending numeric order.

If I use the SQL Convert function, it throws an error when it comes
across a Lot_No with an alpha character.

Any suggestions?

Thanks in advance for your assistance.

  Réponse avec citation
Vieux 24/03/2008, 02h59   #2
Plamen Ratchev
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How To Convert String to Numeric for Sorting?

Here are two methods. The first one assumes you can have at most one
character to the right on some lot numbers. The second will clean all
non-numeric characters and then order. In the second solution replace the
subquery reference to master..spt_values with real utility table with
numbers (http://www.projectdmx.com/tsql/tblnumbers.aspx).

CREATE TABLE Lots (
lot_no NVARCHAR(5) NOT NULL PRIMARY KEY);

INSERT INTO Lots VALUES ('1');
INSERT INTO Lots VALUES ('11');
INSERT INTO Lots VALUES ('12');
INSERT INTO Lots VALUES ('13');
INSERT INTO Lots VALUES ('13A');
INSERT INTO Lots VALUES ('2');
INSERT INTO Lots VALUES ('20');
INSERT INTO Lots VALUES ('21');
INSERT INTO Lots VALUES ('101');
INSERT INTO Lots VALUES ('101A');
INSERT INTO Lots VALUES ('101B');
INSERT INTO Lots VALUES ('102');
INSERT INTO Lots VALUES ('103');

-- SQL Server 2000/2005
SELECT lot_no
FROM Lots
ORDER BY CASE WHEN RIGHT(lot_no, 1) LIKE '[A-Z]'
THEN CAST(LEFT(lot_no, LEN(lot_no) - 1) AS INT)
ELSE CAST(lot_no AS INT)
END, lot_no

-- SQL Server 2005 only
;WITH CleanLots (lot_no, num_lot_no)
AS
(SELECT lot_no,
CAST((SELECT SUBSTRING(lot_no, n, 1)
FROM (SELECT number
FROM master..spt_values
WHERE type = 'P'
AND number BETWEEN 1 AND 100) AS Nums(n)
WHERE n <= LEN(lot_no)
AND SUBSTRING(lot_no, n, 1) LIKE '[0-9]'
FOR XML PATH('')) AS INT)
FROM Lots)
SELECT lot_no
FROM CleanLots
ORDER BY num_lot_no, lot_no;


HTH,

Plamen Ratchev
http://www.SQLStudio.com

  Réponse avec citation
Vieux 24/03/2008, 10h51   #3
-cicada-
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How To Convert String to Numeric for Sorting?

On Mar 24, 12:59pm, "Plamen Ratchev" <Pla...@SQLStudio.com> wrote:
> Here are two methods. The first one assumes you can have at most one
> character to the right on some lot numbers. The second will clean all
> non-numeric characters and then order. In the second solution replace the
> subquery reference to master..spt_values with real utility table with
> numbers (http://www.projectdmx.com/tsql/tblnumbers.aspx).
>
> CREATE TABLE Lots (
> lot_no NVARCHAR(5) NOT NULL PRIMARY KEY);
>
> INSERT INTO Lots VALUES ('1');
> INSERT INTO Lots VALUES ('11');
> INSERT INTO Lots VALUES ('12');
> INSERT INTO Lots VALUES ('13');
> INSERT INTO Lots VALUES ('13A');
> INSERT INTO Lots VALUES ('2');
> INSERT INTO Lots VALUES ('20');
> INSERT INTO Lots VALUES ('21');
> INSERT INTO Lots VALUES ('101');
> INSERT INTO Lots VALUES ('101A');
> INSERT INTO Lots VALUES ('101B');
> INSERT INTO Lots VALUES ('102');
> INSERT INTO Lots VALUES ('103');
>
> -- SQL Server 2000/2005
> SELECT lot_no
> FROM Lots
> ORDER BY CASE WHEN RIGHT(lot_no, 1) LIKE '[A-Z]'
> THEN CAST(LEFT(lot_no, LEN(lot_no) - 1) AS INT)
> ELSE CAST(lot_no AS INT)
> END, lot_no
>
> -- SQL Server 2005 only
> ;WITH CleanLots (lot_no, num_lot_no)
> AS
> (SELECT lot_no,
> CAST((SELECT SUBSTRING(lot_no, n, 1)
> FROM (SELECT number
> FROM master..spt_values
> WHERE type ='P'
> AND number BETWEEN 1 AND 100) AS Nums(n)
> WHERE n <= LEN(lot_no)
> AND SUBSTRING(lot_no, n, 1)LIKE '[0-9]'
> FOR XML PATH('')) AS INT)
> FROM Lots)
> SELECT lot_no
> FROM CleanLots
> ORDER BY num_lot_no, lot_no;
>
> HTH,
>
> Plamen Ratchevhttp://www.SQLStudio.com


Plamen,

Thanks for the prompt response. this solution works fine when it is
included as part of the SELECT statement:

SELECT *
FROM tblData
ORDER BY CASE WHEN RIGHT(lot_no, 1) LIKE '[A-Z]'
THEN CAST(LEFT(lot_no, LEN(lot_no) - 1) AS INT)
ELSE CAST(lot_no AS INT)
END, lot_no

However on the page I allow the user to select the sort order, with
the default sort order being by Lot_No, as defined here:

<CFPARAM name="DefOrder" default= Lot_No>

However if I change the above line to:

<CFPARAM name="DefOrder" default= CASE WHEN RIGHT(lot_no, 1) LIKE '[A-
Z]'
THEN CAST(LEFT(lot_no, LEN(lot_no) - 1) AS INT)
ELSE CAST(lot_no AS INT)
END, lot_no>

I receive an error message, as a result of inclusion of the brackets
in the expression.

Thanks again

John
  Réponse avec citation
Vieux 24/03/2008, 14h14   #4
Plamen Ratchev
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How To Convert String to Numeric for Sorting?

I am really not sure what mechanism is used on your page to pass down the
ORDER BY to the query. A few approaches that may work:

1). Create a computed column in your table the has the CASE expression as
the definition. Then set the computed column name as default order column in
your page.

ALTER TABLE Lots
ADD num_lot_no AS (CASE WHEN RIGHT(lot_no, 1) LIKE '[A-Z]'
THEN CAST(LEFT(lot_no, LEN(lot_no) - 1)
AS INT)
ELSE CAST(lot_no AS INT)
END)

2). Create a view that has the column definition, then use the view on your
page.

CREATE VIEW CleanLots (lot_no, num_lot_no)
AS
SELECT lot_no,
CASE WHEN RIGHT(lot_no, 1) LIKE '[A-Z]'
THEN CAST(LEFT(lot_no, LEN(lot_no) - 1) AS INT)
ELSE CAST(lot_no AS INT)
END
FROM Lots

3). Use a derived table to define the column in the query, then hopefully
your mechanism for passing the ORDER BY will apply it over the derived
table.

SELECT lot_no
FROM (SELECT lot_no,
CASE WHEN RIGHT(lot_no, 1) LIKE '[A-Z]'
THEN CAST(LEFT(lot_no, LEN(lot_no) - 1) AS INT)
ELSE CAST(lot_no AS INT)
END
FROM Lots) AS L(lot_no, num_lot_no)

HTH,

Plamen Ratchev
http://www.SQLStudio.com

  Réponse avec citation
Vieux 24/03/2008, 17h01   #5
Erland Sommarskog
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How To Convert String to Numeric for Sorting?

-cicada- (johnfurphy@a1.com.au) writes:
><CFPARAM name="DefOrder" default= Lot_No>
>
> However if I change the above line to:
>
><CFPARAM name="DefOrder" default= CASE WHEN RIGHT(lot_no, 1) LIKE '[A-
> Z]'
> THEN CAST(LEFT(lot_no, LEN(lot_no) - 1) AS INT)
> ELSE CAST(lot_no AS INT)
> END, lot_no>
>
> I receive an error message, as a result of inclusion of the brackets
> in the expression.


What about actually posting the error message? Wether you actually
do something like the above, I don't know, but I don't know ASP or
ASP .Net.

But wouldn't you at least have to put the entire SQL-expression in
double quotes? You may also have to replace [] with tokens starting
with &. On the top of my head, I don't know what the names for the
brackets are in HTML.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
  Réponse avec citation
Vieux 25/03/2008, 04h56   #6
Tom van Stiphout
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How To Convert String to Numeric for Sorting?

On Mon, 24 Mar 2008 16:01:06 +0000 (UTC), Erland Sommarskog
<esquel@sommarskog.se> wrote:

Sorry - CFPARAM suggests Cold Fusion, not ASP*.
The OP might want to continue this discussion in a CF group.
-Tom.


>-cicada- (johnfurphy@a1.com.au) writes:
>><CFPARAM name="DefOrder" default= Lot_No>
>>
>> However if I change the above line to:
>>
>><CFPARAM name="DefOrder" default= CASE WHEN RIGHT(lot_no, 1) LIKE '[A-
>> Z]'
>> THEN CAST(LEFT(lot_no, LEN(lot_no) - 1) AS INT)
>> ELSE CAST(lot_no AS INT)
>> END, lot_no>
>>
>> I receive an error message, as a result of inclusion of the brackets
>> in the expression.

>
>What about actually posting the error message? Wether you actually
>do something like the above, I don't know, but I don't know ASP or
>ASP .Net.
>
>But wouldn't you at least have to put the entire SQL-expression in
>double quotes? You may also have to replace [] with tokens starting
>with &. On the top of my head, I don't know what the names for the
>brackets are in HTML.

  Réponse avec citation
Vieux 25/03/2008, 12h25   #7
Erland Sommarskog
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How To Convert String to Numeric for Sorting?

Tom van Stiphout (no.spam.tom7744@cox.net) writes:
> On Mon, 24 Mar 2008 16:01:06 +0000 (UTC), Erland Sommarskog
><esquel@sommarskog.se> wrote:
>
> Sorry - CFPARAM suggests Cold Fusion, not ASP*.
> The OP might want to continue this discussion in a CF group.
> -Tom.


Well, I don't know Cold Fusion either, as you can tell. :-)

Yes, if my uninitated guesses about SGML in general do not , John
should find a forum for his web environment, whatever that may be.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 21h29.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16145 seconds with 15 queries