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

Posted on 2007-08-10
Last Modified: 2012-08-13

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".

Question by:heathwallace
    LVL 15

    Expert Comment

    ORDER BY COALESCE(CAST(Section AS float), 0) ASC
    LVL 15

    Accepted Solution

    order by
    case ISNUMERIC(selection)
          when 0 then 0
          else cast(selection as float)
    end desc

    Author Comment

    I've heard ISNUMERIC isn't very reliable, is it likely to be a problem in this case?
    LVL 15

    Expert Comment

    i never heard it was unreliable. always worked for me

    Author Comment

    Thanks, it seems to be working fine.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This is my first ever Article on EE or elsewhere; therefore, please bear with me if I have some discrepancies in my writing. I read many articles and questions related to "how to pass values to SSIS packages at run-time?"  Hence, this common ques…
    Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
    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.
    Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now