Sorting Alphanumeric field in SQL CE (Compact Edition) version 3.5

后端 未结 2 781
小鲜肉
小鲜肉 2021-01-21 02:56

Sorting Alphanumeric field in SQL CE (Compact Edition) version 3.5

TreeNumber is a nvarchar field with a mix of numbers and strings for the values. I want to sort these

相关标签:
2条回答
  • 2021-01-21 03:22

    Are functions supported in CE? You could make your own IsNuemric function (an easy char by char parser, for example) and call it later in your query

    0 讨论(0)
  • 2021-01-21 03:24

    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 tally table (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

    Create the alphabet table (temp for demo purposes):

    select *
    into #alphatable
    from
    (
    
    select 'A' as alpha union all
    select 'B' union all
    select 'C' union all
    select 'D'
    --etc. etc.
    ) x
    

    Create a tree table (temp for demo purposes):

    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
    

    The solution:

    select TreeNumber
    from
    (
    select t.*, tr.*, substring(TreeNumber, case when N >  len(TreeNumber) then len(TreeNumber) else N end, 1) as singleChar
    from tally t
    cross join #tree tr
    where t.N < (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 
    

    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.

    0 讨论(0)
提交回复
热议问题