January 15, 2010 at 6:18 am
I have a dataset that looks like this:
100
101
200
201
300
301
AAA
AAB
AAC
BBA
BBB
BBC
CCA
CCB
etc.
I would like to have the numeric values to come after the alpha. I still want everything ASC. I just want the numeric values to come after the alpha values. I could easily split it into two queries, but I need this to be one query with one dataset.
Any suggestions?
January 15, 2010 at 6:23 am
try using ISNUMERIC in the order by
SELECT * FROM yourTable
ORDER BY ISNUMERIC(yourCol), yourCol
January 15, 2010 at 6:32 am
SSComitted thanks...we're almost there.
That worked. But, I have the added complication of SELECT DISTINCT and it wants me to put isnumeric(search_phrase) in the select statement.
Here's my query...maybe this will help.
SELECT DISTINCT search_phrase FROM chief_search_results WHERE (results > 0) ORDER BY isnumeric(search_phrase), search_phrase
I can't really put isnumeric(search_phrase)
in the select statement...can I?
January 15, 2010 at 6:35 am
Disregard...haven't had my coffee yet...put isnumeric(search_phrase) in the select statement...it works like a champ...thanks a billion!
January 15, 2010 at 6:37 am
SELECT MyString, ASCII(MyString)
FROM (
SELECT '000' AS MyString UNION ALL
SELECT '101' UNION ALL
SELECT '200' UNION ALL
SELECT '201' UNION ALL
SELECT '300' UNION ALL
SELECT '901' UNION ALL
SELECT ' aAA' UNION ALL
SELECT 'AAB' UNION ALL
SELECT 'AAC' UNION ALL
SELECT 'BBA' UNION ALL
SELECT 'BBB' UNION ALL
SELECT 'BBC' UNION ALL
SELECT 'CCA' UNION ALL
SELECT 'CCB'
) d
ORDER BY CASE WHEN ASCII(MyString) BETWEEN 48 AND 57 THEN 'Number' ELSE 'Alpha' END, MyString
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
January 15, 2010 at 7:45 pm
Uh-huh.... and what would you want to do with something like 1d3 and what do you think ISNUMERIC will return for that? Do not use ISNUMERIC as an ISALLDIGITS function because that's simply not what is was designed for. If you want an ISALLDIGITS bit of functionality, you'll need to use LIKE and a double negative...
WHERE somecolumn NOT LIKE '%[^0-9]%'
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply