在SQL CE(精简版)版本3.5

问题描述:

排序字母数字字段在SQL CE字母排序字段(精简版)版本3.5在SQL CE(精简版)版本3.5

TreeNumber是与价值观数字和字符串的混合一个nvarchar的领域。我想排序这些记录,以便包含字母字符的记录位于顶部,其余按数字顺序排序。

我想类似下面的查询其在SQL Server中工作的东西:

SELECT * FROM Tree 
ORDER BY 
    (CASE WHEN TreeNumber LIKE '%[a-z]%' THEN 0 ELSE TreeNumber END), TreeNumber 

上面的查询似乎并没有工作,因为[]范围不CE支持。另一个与SQL Server一起使用但在CE中不起作用的解决方案如下:

SELECT * FROM Tree 
ORDER BY 
    (CASE IsNumeric(TreeNumber) WHEN 0 THEN 0 ELSE TreeNumber END), TreeNumber 

好的,这个解决方案很丑,不适合心脏不好的人。我没有在SQL CE上测试,但它只使用基本的t-sql,所以它应该没问题。你将不得不创建一个tally table(如果你不想读它,只需运行他的第一个代码块,它使用tempdb,所以你需要改变它)和一个表来保存字母表中的每个字母(由于缺乏模式匹配功能)。创建理货表后(您不必一路走到11000,如示例所示),运行这些表,您将看到您想要的排序行为

创建字母表(用于演示目的的温度):

select * 
into #alphatable 
from 
(

select 'A' as alpha union all 
select 'B' union all 
select 'C' union all 
select 'D' 
--etc. etc. 
) x 

创建树表(临时用于演示目的):

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 

解决办法:

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 

这基本上是Moden描述为“跨越字符”的技术,然后每个字符都加入到alpha表中。在alpha表中没有行的行是数字。

CE中是否支持函数?你可以创建你自己的IsNuemric函数(例如char char),稍后在你的查询中调用它。