-4

I have a table which have field name ID. My data showing like below

LE001001
LE0010010
LE001003
LE001005
LE001006
LE001007
LE001008
LE001009

But I want to show my data by sorting like below

LE001001
LE001003
LE001005
LE001006
LE001007
LE001008
LE001009
LE0010010

Please help me to crate query

James Walker
  • 11
  • 2
  • 3
  • Where would you place LE001010? Do you consider an ID to have three parts; two letters, a five digit number and a number of unknown digits? – Thorsten Kettner Jan 05 '15 at 13:21

3 Answers3

0

You want to sort by the number after 'LE'. Assuming the prefix is always the same length, you can use a trick where you order by the length and then the value:

order by len(col), col

This works for the sample data in the question. You can do explicit conversions for this:

order by left(col, 2),
         cast(ltrim(rtrim(substring(col, 3, len(col)))) as int)

But the first method is easier to type if it works.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Another anonymous downvote on a correct answer. – Gordon Linoff Jan 05 '15 at 13:43
  • 9
    I wasn't the one who downvoted you, and your answer could be correct, I don't really know. But maybe a 250k user should be closing obvious duplicates rather than answering them. – Madara's Ghost Jan 05 '15 at 13:48
  • @SecondRikudo . . . First, it didn't only affect me. It appeared to affect every answer on the question. Second, I have a different (albeit quite a minority opinion) on what constitutes a duplicate. – Gordon Linoff Jan 05 '15 at 13:56
  • 7
    That is your right. Just don't be surprised when you get downvoted by the majority. I find your answer not helpful in the larger sense that now someone from google looking for "sorting query in sql server" will find your specific answer that won't help him at all. – Madara's Ghost Jan 05 '15 at 13:58
  • @SecondRikudo . . . If such downvotes are going to be effective, they should include a comment explaining why. Wantonly downvoting answers basically communicate no information. Anonymous downvotes on *correct* answers is downright misleading. – Gordon Linoff Jan 05 '15 at 14:09
  • 7
    Your definition of effective is different than mine. I use downvotes as an effective tool to make not useful questions and answers disappear with low view rate. I don't need to post a comment for that. And even as you got an explanation on why your answer was downvoted, you engaged in argument and caused me to write two more comments, effectively wasting my productive time. So you shouldn't be surprised why people wouldn't want to comment on your answers when they downvote you. – Madara's Ghost Jan 05 '15 at 14:21
  • @SecondRikudo . . . You should then remove the comment that says that you didn't downvote me. – Gordon Linoff Jan 05 '15 at 14:22
  • 4
    @GordonLinoff It's rare that anyone would ever leave a comment as to why they downvote a post, it just leads to arguments. – Taryn Jan 05 '15 at 14:39
  • @GordonLinoff I have one more question can you please help me? – James Walker Jan 05 '15 at 16:55
  • @GordonLinoff I get every day a down Vote on a 0 accepted answer since 3 days, so just don't worry about these -1 :D It all will become good on one or the other day – Rizier123 Jan 14 '15 at 11:43
0
order by substr(colum_name, 8, 2)
starko
  • 1,150
  • 11
  • 26
-1

You can define 2 columns. One for number 1001, 10010 and the other - formula which will add the number to string LE. Then you sort the first column as integer number and use the compound column LE001001 for other needs.

i486
  • 6,491
  • 4
  • 24
  • 41