473,326 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Ordering varchar column numerically

Is there a preferred way to order a varchar column numerically (for those
that are numeric), then alphanumerically for all others?

I've tried:

ORDER BY CASE WHEN IsNumeric(<column_name>) = 1 THEN CONVERT(Float,
<column_name>) ELSE 999999999 END;

and

ORDER BY CASE WHEN IsNumeric(<column_name>) = 1 THEN 0 ELSE 1 END, CASE WHEN
IsNumeric(<column_name>) = 1 THEN CONVERT(Float, <column_name>) ELSE 999999
END, <column_name>;

Neither of these however give the desired results.

Any ideas?

Thanks,
Frank
Jul 23 '05 #1
4 4736
It would be useful to see some sample data - are your 'numeric' data
integers, decimals, or floats; are there negative numbers in the data,
etc.? Assuming that your data is made up of either positive integers or
strings, then this should work:

select *
from dbo.MyTable
order by case when MyColumn like '%[^0-9]%' then 1 else 0 end, MyColumn

ISNUMERIC() might work, but it considers so many things to be numbers
that you may not get the results you want:

select *
from dbo.MyTable
order by isnumeric(MyColumn), col1

If this doesn't help, then please post DDL and sample data:

http://www.aspfaq.com/etiquette.asp?id=5006

Simon

Jul 23 '05 #2
Assuming your numerics are just positive integers, try:

SELECT col
FROM T1
ORDER BY
CASE WHEN col LIKE '%[^0-9]%'
THEN 9E99
ELSE CAST(col AS INTEGER) END
, col

--
David Portas
SQL Server MVP
--

Jul 23 '05 #3

"Simon Hayes" <sq*@hayes.ch> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
It would be useful to see some sample data - are your 'numeric' data
integers, decimals, or floats; are there negative numbers in the data,
etc.? Assuming that your data is made up of either positive integers or
strings, then this should work:

select *
from dbo.MyTable
order by case when MyColumn like '%[^0-9]%' then 1 else 0 end, MyColumn
Thank you Simon, the above syntax worked like a charm!

Frank
ISNUMERIC() might work, but it considers so many things to be numbers
that you may not get the results you want:

select *
from dbo.MyTable
order by isnumeric(MyColumn), col1

If this doesn't help, then please post DDL and sample data:

http://www.aspfaq.com/etiquette.asp?id=5006

Simon

Jul 23 '05 #4

"David Portas" <RE****************************@acm.org> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Assuming your numerics are just positive integers, try:

SELECT col
FROM T1
ORDER BY
CASE WHEN col LIKE '%[^0-9]%'
THEN 9E99
ELSE CAST(col AS INTEGER) END
, col

--
David Portas
SQL Server MVP
--


Thanks David, this one actually works better. If I use:

order by case when <mycolumn> like '%[^0-9]%' then 1 else 0 end, <mycolumn>

It does not correctly order the integers (i.e. 1,10,11,12,2,3,4,...), but
does correctly order the alphnumerics. Using your suggestion, it works for
both cases.

Frank
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: dmhendricks | last post by:
Greetings, I have a question. I work on some SQL2k/ASP.NET apps at work. My predacessor, who created the databases/tables seemed to have liked to use 'char' for all text fields. Is there a...
9
by: Rick | last post by:
I've created a clustered two column index on a table where the second column is an integer value. When the first column is the same, instead of ordering in numerical order it is ordering...
2
by: D. Dante Lorenso | last post by:
First I created a function that selected the next available pin code from a table of pre-defined pin codes: CREATE FUNCTION "public"."get_next_pin_code" () RETURNS varchar AS' DECLARE...
2
by: Alexandre H. Guerra | last post by:
I needed to log all statements executed during a period of time and now i need ordering the long varchar column in the statements monitor table (STMT_TEXT) Is there any flag to set to release...
7
by: James o'konnor | last post by:
hello. i have the next for create one table into db2 CREATE TABLE "MYSQUEMA"."TABLADEMO" ( "ID" INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY ( START WITH +0 INCREMENT BY +1 MINVALUE +0...
6
by: mike | last post by:
so I keep optimizing my fields down to the minimum character length necessary i.e., varchar(15), then I find out a month later its gotta get bigger, then a few months later, bigger again, etc. ...
0
Krishna Ladwa
by: Krishna Ladwa | last post by:
In Sql Server 2000 Version, I found that no Notification message box appears when converting text column to varchar but the data gets truncated to the given size for the varchar. Whereas it appears...
4
by: Nick Chan | last post by:
all these while i've only used varchar for any string i heard from my ex-boss that char helps speed up searches. is that true? so there are these: 1) char with index 2) char without index...
0
by: maheshmohta | last post by:
Background Often while remodeling legacy application, one of the important tasks for the architects is to have an optimum usage of storage capabilities of database. Most of the legacy applications...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.