473,765 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Ordering varchar column numerically

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

I've tried:

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

and

ORDER BY CASE WHEN IsNumeric(<colu mn_name>) = 1 THEN 0 ELSE 1 END, CASE WHEN
IsNumeric(<colu mn_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 4765
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(MyCol umn), 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.goo glegroups.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(MyCol umn), 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.goo glegroups.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
29498
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 reason why he would have done this over using varchar? It's a minor annoyance to always have to RTRIM data and it makes directly making changes to the database more annoying (with all the pointless trailing spaces)? I usually use char for fixed...
9
2075
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 1,10,100,2,20,200 etc. How can I change the behaviour to order the data in numerical order? Thanks, Rick
2
2564
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 my_pin_code VARCHAR; BEGIN ... /* this is the pincode we just fetched */ RETURN (my_pin_code);
2
4109
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 the DB2 255 char wide ordering limit ? Thanks Alexandre
7
4784
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 MAXVALUE +2147483647 NO CYCLE
6
12909
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. Nowadays on sql server 2005 and on, how bad is it really to use varchar(max)? Is there really a big performance or storage hit or is it negligible? -Mike
0
1742
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 when you convert the varchar column to text column. Do this through Enterprise Manager Console Create a New table with a column as varchar datatype from Enterprise Manager  table created  Open the table and add one row  Successfully added the...
4
8869
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 3) char with clustered index
0
27257
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 are constrained by the technology available at the time of their development and hence aren’t optimum as per current scenario. One of such cases is the extensive usage of CHAR fields, which aren’t optimum solution for space storage now. This paper...
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9957
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8832
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.