Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, this solution is ugly, and not for the faint of heart. I haven't tested on SQL CE, but it only uses basic t-sql so it should be ok. You will have to create a <a href="http://www.sqlservercentral.com/articles/T-SQL/62867/" rel="nofollow">tally table</a> (just run his first block of code, if you don't want to read it. It uses the tempdb so you'll want to change that), and a table to hold each letter of the alphabet (due to lack of pattern matching functions). After creating the tally table (you don't have to go all the way to 11000 as the example shows), run these, and you'll see the sorting behavior you want</p> <p>Create the alphabet table (temp for demo purposes):</p> <pre><code>select * into #alphatable from ( select 'A' as alpha union all select 'B' union all select 'C' union all select 'D' --etc. etc. ) x </code></pre> <p>Create a tree table (temp for demo purposes):</p> <pre><code>select * into #tree from ( select 'aagew' as TreeNumber union all select '3' union all select 'bsfreww' union all select '1' union all select 'xcaswf' ) x </code></pre> <p>The solution:</p> <pre><code>select TreeNumber from ( select t.*, tr.*, substring(TreeNumber, case when N &gt; len(TreeNumber) then len(TreeNumber) else N end, 1) as singleChar from tally t cross join #tree tr where t.N &lt; (select max(len(TreeNumber)) from #tree) ) z left join #alphatable a on z.singlechar = a.alpha group by TreeNumber order by case when max(alpha) is not null then 0 else TreeNumber end </code></pre> <p>This is basically the technique that Moden describes as "Stepping through the characters", then each character is joined on the alpha table. Rows with no row in the alpha table are numeric. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload