[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

CAST/CONVERT nvarchar to float for sorting, but only if a number

Posted on 2007-08-10
5
Medium Priority
?
9,891 Views
Last Modified: 2012-08-13
Hi,

I have a SQL Server 2000 database with an nvarchar column containing a mixture of strings, e.g. "Introduction", "Appendix A" and section numbers, e.g. "2.3", "9.1" or "10.2",

I need to be able to sort this column in my SELECT statement but because the type is nvarchar, the numbers are being sorted as strings, so "10" is coming before "1".

I am looking for a way to be able to sort these values so that text values come at either the beginning or end of the list (there are only a few so it doesn't matter how they're ordered) and numbers are listed in numerical order.

Here's an example of what I'm trying:

SELECT Section AS 'SectionAsNVarChar'
ORDER BY CAST(ISNULL(Section, '0') AS float) ASC

The idea of the above is to list the sections using their string representation, but sort them by their numeric value.

Where I'm stuck, is finding a way of saying "if the value can't be CAST into a float, set it to 0".

Tom
0
Comment
Question by:heathwallace
  • 2
  • 2
5 Comments
 
LVL 15

Expert Comment

by:dbbishop
ID: 19670245
ORDER BY COALESCE(CAST(Section AS float), 0) ASC
0
 
LVL 15

Accepted Solution

by:
derekkromm earned 2000 total points
ID: 19670272
order by
case ISNUMERIC(selection)
      when 0 then 0
      else cast(selection as float)
end desc
0
 

Author Comment

by:heathwallace
ID: 19670312
I've heard ISNUMERIC isn't very reliable, is it likely to be a problem in this case?
0
 
LVL 15

Expert Comment

by:derekkromm
ID: 19670320
i never heard it was unreliable. always worked for me
0
 

Author Comment

by:heathwallace
ID: 19670346
Thanks, it seems to be working fine.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question