Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Order by Caps first and order by asc

$
0
0

Hi SQL Gurus,

I have a requirement to order by a job title with CAPS in it as first and do the rest in ascending.

For simplicity I have populated only job title starts with "a". So for job title starts with "b" should go after all the "a" completes. Somehow was able to work out as per the business requirement, but unable to understand what it makes to work like that.

if OBJECT_ID('tempdb..#test') > 0
drop table #test
create table #test(id int identity(1,1),job_title varchar(100))

insert into #test
select 'Account Relations Liaison' union
select 'Account Relations Liaison II' union
select 'Accountant, Primonics' union
select 'Accounting Services Assistant' union
select 'AP Lead' union
select 'AP Processor' union
select 'Accountant II - Corporate Accountant' union
select 'Accountant II - Fixed Asset Accountant' union
select 'Accountant II - Inventory Accountant' union
select 'Accountant I' union
select 'Auditor II' union
select 'Associate Project Manager' union
select 'Assistant Controller'

select job_title from #test order by job_title
--Account Relations Liaison
--Account Relations Liaison II
--Accountant I
--Accountant II - Corporate Accountant
--Accountant II - Fixed Asset Accountant
--Accountant II - Inventory Accountant
--Accountant, Primonics
--Accounting Services Assistant
--AP Lead
--AP Processor
--Assistant Controller
--Associate Project Manager
--Auditor II

--works as per the business requirement
select job_title from #test order by convert(binary,job_title)
--AP Lead
--AP Processor
--Account Relations Liaison
--Account Relations Liaison II
--Accountant I
--Accountant II - Corporate Accountant
--Accountant II - Fixed Asset Accountant
--Accountant II - Inventory Accountant
--Accountant, Primonics
--Accounting Services Assistant
--Assistant Controller
--Associate Project Manager
--Auditor II


Ganesh


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>