SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 order by for alpha numeric as alpha first then num
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

joseln
Starting Member

11 Posts

Posted - 11/23/2007 :  03:03:59  Show Profile  Reply with Quote
Hi,

I have a field value in my table as

1
a
b
2
c
3

but i want to display in my select query as

a
b
c
1
2
3

pls suggest how to write the "order by" in my select query.

thanks

harsh_athalye
Flowing Fount of Yak Knowledge

India
5581 Posts

Posted - 11/23/2007 :  03:15:29  Show Profile  Visit harsh_athalye's Homepage  Click to see harsh_athalye's MSN Messenger address  Send harsh_athalye a Yahoo! Message  Reply with Quote
Something like this:


-- Prepare Sample data
declare @t table
(
	a varchar(10)
)

insert @t
select '1' union all
select 'a' union all
select 'b' union all
select '2' union all
select 'c' union all
select '3'

-- Final query
select * from @t
order by case when a like '[a-z]' then 1 else 2 end, a


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

joseln
Starting Member

11 Posts

Posted - 11/23/2007 :  03:34:03  Show Profile  Reply with Quote
Hi, Thanks

by the way, what 1 and 2 represent in the case statement of the order by clause.

case when a like [a-z] then 1 else 2 end

thanks again
Go to Top of Page

harsh_athalye
Flowing Fount of Yak Knowledge

India
5581 Posts

Posted - 11/23/2007 :  03:49:46  Show Profile  Visit harsh_athalye's Homepage  Click to see harsh_athalye's MSN Messenger address  Send harsh_athalye a Yahoo! Message  Reply with Quote
1 and 2 represents..1 and 2 only!!

You can consider them as rank since you want Alphabets higher in the order, I ranked them as rank 1 and anything other than that as rank 2.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22769 Posts

Posted - 11/23/2007 :  04:28:35  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
What if you have value like 'ab5'?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

harsh_athalye
Flowing Fount of Yak Knowledge

India
5581 Posts

Posted - 11/23/2007 :  04:36:01  Show Profile  Visit harsh_athalye's Homepage  Click to see harsh_athalye's MSN Messenger address  Send harsh_athalye a Yahoo! Message  Reply with Quote
Well in that case OP has to tell what should be the ranking of 'ab5'? Whether along with alphabets, between alphabets and numbers or after numbers?

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30281 Posts

Posted - 11/23/2007 :  06:24:26  Show Profile  Visit SwePeso's Homepage  Reply with Quote
select * from @t
order by case when a not like '%[^0-9]%' then 2 else 1 end, len(a), a



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000