Sort column numerically when type is nvarchar

Hi i have in my database

SUB_ID nvarchar(3)

so i end up with    0
                    1
                   10
                   20
                    2

And i need to be able to get 0,1,2,10,20

Any suggestions
LVL 7
TRACEYMARYAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HilaireCommented:
select * from <yourtable>
ORDER BY CAST(SUB_ID AS INT)
0
svidCommented:
select columns
from table
where isnumeric(sub_id) = 1
order by cast(sub_id as decimal(10,2))
0
LowfatspreadCommented:
order by right("000" + sub_id ,3)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

TRACEYMARYAuthor Commented:
Oh it could be Alpha i.e a

0
HilaireCommented:
Lowfatspread's code is robust and handles both numeric and alpha.
Just use single quotes instead of double quotes, and a N for the string constant to avoid automatic conversion

order by right(N'000' + sub_id ,3)

Please don't accept this comment as an answer,
this is merely a comment on Lowfatspread's suggestion.
0
TRACEYMARYAuthor Commented:
What is N for
order by right(N'000' + sub_id ,3)
0
sigmaconCommented:
N makes a string a Unicode Character String (like nvarchar). Make sure you cover as many digits as your numbers can be long, e.g. 10 digits if you store equivalents of integers:

order by right(N'0000000000' + sub_id, 10)

the above assumes you have empty strings '' in your column
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TRACEYMARYAuthor Commented:
i tired
order by right(N'000' + WORKORDER_sub_id, 10)

but i get
0
1
10
11
2
0
LowfatspreadCommented:
order by right(N'0000000000' + WORKORDER_sub_id, 10)

but you started off saying the column was length 3
so
order by right(N'000' + WORKORDER_sub_id, 3) should work...

if it doesn't

can you do a

select '(' + workorder_sub_id +')'
where workorder_sub_id like '%1%'
order by 1

and post the results

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.