| Author |
Topic |
|
bridge
Yak Posting Veteran
93 Posts |
Posted - 2005-09-01 : 08:31:43
|
| select 'Unit_Number' = case when isnumeric(unit_number) = 1 then unit_number when isnumeric(unit_number) = 0 then cast(unit_number as int) endfrom MyTableThe select statement gives error, can anybody tell me why is this giving error? |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-09-01 : 08:39:28
|
| What is the error you got?Instead of isnumeric, use Like '%[0-9]%'select 'Unit_Number' = case when unit_number like '%[0-9]%' then unit_numberelse cast(unit_number as int) end from MyTableMadhivananFailing to plan is Planning to fail |
 |
|
|
bridge
Yak Posting Veteran
93 Posts |
Posted - 2005-09-01 : 08:50:27
|
| The error I got isServer: Msg 245, Level 16, State 1, Line 1Syntax error converting the varchar value '>0001' to a column of data type int.I actually want to cast this column into int for sorting purpose. Because this column can contain both values like 1,2,3,4,5 and >1000, I want to display all numeric values in an order. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-09-01 : 08:56:34
|
| If unit_number is varchar datatype thenTry thisSelect unit_number as 'unit_number' from yourTable order by len(unit_number), unit_number Otherwise,Post the table structure, some sample data and the result you wantMadhivananFailing to plan is Planning to fail |
 |
|
|
bridge
Yak Posting Veteran
93 Posts |
Posted - 2005-09-01 : 09:05:09
|
| when we use len(unit_number) its not acceptable when using DISTINCT. What is the problem with ISNUMERIC? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-09-01 : 09:08:53
|
| I repeat what I asked youPost the table structure, some sample data and the result you wantMadhivananFailing to plan is Planning to fail |
 |
|
|
bridge
Yak Posting Veteran
93 Posts |
Posted - 2005-09-01 : 09:27:35
|
| I want the output in orderdeclare @Test table (Unit_Number varchar(255))insert into @Testselect 1 union allselect 10 union allselect 999 union allselect 2 union allselect 3 union allselect 4 union allselect 11 union allselect 100 union allselect 101 union allselect 1000 union allselect '>0001' union allselect '>0002'select * from @Test |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-09-01 : 09:34:15
|
| WellWhat is the output you want from those?MadhivananFailing to plan is Planning to fail |
 |
|
|
bridge
Yak Posting Veteran
93 Posts |
Posted - 2005-09-01 : 09:58:23
|
| 123410111001019991000while the >0001 and 2 should be below |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-09-01 : 10:08:47
|
| Thats what I suggested alreadydeclare @Test table (Unit_Number varchar(255))insert into @Testselect 1 union allselect 10 union allselect 999 union allselect 2 union allselect 3 union allselect 4 union allselect 11 union allselect 100 union allselect 101 union allselect 1000 union allselect '>0001' union allselect '>0002'select * from @Test order by len(Unit_Number),Unit_NumberMadhivananFailing to plan is Planning to fail |
 |
|
|
bridge
Yak Posting Veteran
93 Posts |
Posted - 2005-09-02 : 01:08:03
|
| execute thisdeclare @Test table (Unit_Number varchar(255))insert into @Testselect 1 union allselect 10 union allselect 10 union allselect 999 union allselect 2 union allselect 3 union allselect 4 union allselect 11 union allselect 100 union allselect 101 union allselect 1000 union allselect '>0001' union allselect '>0002'select distinct unit_number from @Test order by len(Unit_Number),Unit_Number |
 |
|
|
Xerxes
Aged Yak Warrior
666 Posts |
Posted - 2005-09-02 : 01:14:19
|
Let me understand this....you want to sort characters ('>0001' & '>0002') and numerics together?Couldn't you just normalize your data? What prevents you from converting the column of figures by CAST? You mentioned it earlier.Semper fi, XERXES, USMC(Ret.)------------------------------------------------------The Marine Corps taught me everything but SQL! |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-09-02 : 01:32:49
|
| Here is a querySelect * from (Select distinct unit_number from @test) T order by len(unit_number),unit_numberBut everytime you give only part of result and not exact oneWhenever you ask questions, post your full requirement clearlyMadhivananFailing to plan is Planning to fail |
 |
|
|
|