从SQL Server CE中的字符串中获取最后一个索引值

时间:2016-12-15 09:44:04

标签: sql-server-ce

我正在使用SQL Server Compact Edition作为我的应用程序的数据库。

我有一个字符串

C:\wwsoftware\Application Server\3.0.000\3.0.001\IAS 3.0 P01.zip

我需要从该字符串中获取IAS 3.0 P01.zip值。

我可以使用SQL Server来完成此任务,但无法在SQL Server CE中执行此任务。

请帮助我。

提前致谢。

2 个答案:

答案 0 :(得分:1)

cordova plugin rm cordova-plugin-console

答案 1 :(得分:1)

3N1GM4 已经要求您澄清在SQL Server中成功使用的方法,其次,您没有分享该信息,也没有详细说明为什么不起作用。

我将在这里说出你正在使用REVERSE()函数来反转字符串和CHARINDEX()然后找到第一个反斜杠。

与SQL Server SQL Server Compact 4.0 does not have a REVERSE() function不同,这使得提取文件名的最终反斜杠变得非常困难。

SQL Server Compact中缺少存储过程意味着它是另一种无法使用的途径。

ErikEJ 提供了一个很好的替代方案,即使用.NET Framework来完成繁重的工作。如果您具有访问SQL Server Compact数据库的.NET应用程序代码,那么您可以在.NET代码本身中提取文件名。我会建议ErikEJ的方法是最好的。

如果你坚持留在SQL Server Compact领域,那么纯粹在SQL中的唯一选择将包括粗糙的hacks或“臭”代码。

我一般不提倡或支持这种方法,但是如果你想要了解如何解决这个问题,那么就会出现以下SQL语句 - 它会尝试识别反斜杠在第一个字母,下一个字母等位置的位置,然后使用MAX()函数找到最后一个反斜杠的位置。然后使用它,然后使用SUBSTRING()函数提取文件名。

-- I do not recommend this crude approach! 
SELECT SUBSTRING(PATH, LAST_BACKSLASH + 1, LEN(PATH)) 
FROM ( 
SELECT MAX(CHARINDEX('\', PATH_QUERY.PATH, NUMBER)) as LAST_BACKSLASH, PATH_QUERY.PATH as PATH 
FROM ( 
SELECT 0 as NUMBER UNION ALL 
SELECT 1 as NUMBER UNION ALL 
SELECT 2 as NUMBER UNION ALL 
SELECT 3 as NUMBER UNION ALL 
SELECT 4 as NUMBER UNION ALL 
SELECT 5 as NUMBER UNION ALL 
SELECT 6 as NUMBER UNION ALL 
SELECT 7 as NUMBER UNION ALL 
SELECT 8 as NUMBER UNION ALL 
SELECT 9 as NUMBER UNION ALL 
SELECT 10 as NUMBER UNION ALL 
SELECT 11 as NUMBER UNION ALL 
SELECT 12 as NUMBER UNION ALL 
SELECT 13 as NUMBER UNION ALL 
SELECT 14 as NUMBER UNION ALL 
SELECT 15 as NUMBER UNION ALL 
SELECT 16 as NUMBER UNION ALL 
SELECT 17 as NUMBER UNION ALL 
SELECT 18 as NUMBER UNION ALL 
SELECT 19 as NUMBER UNION ALL 
SELECT 20 as NUMBER UNION ALL 
SELECT 21 as NUMBER UNION ALL 
SELECT 22 as NUMBER UNION ALL 
SELECT 23 as NUMBER UNION ALL 
SELECT 24 as NUMBER UNION ALL 
SELECT 25 as NUMBER UNION ALL 
SELECT 26 as NUMBER UNION ALL 
SELECT 27 as NUMBER UNION ALL 
SELECT 28 as NUMBER UNION ALL 
SELECT 29 as NUMBER UNION ALL 
SELECT 30 as NUMBER UNION ALL 
SELECT 31 as NUMBER UNION ALL 
SELECT 32 as NUMBER UNION ALL 
SELECT 33 as NUMBER UNION ALL 
SELECT 34 as NUMBER UNION ALL 
SELECT 35 as NUMBER UNION ALL 
SELECT 36 as NUMBER UNION ALL 
SELECT 37 as NUMBER UNION ALL 
SELECT 38 as NUMBER UNION ALL 
SELECT 39 as NUMBER UNION ALL 
SELECT 40 as NUMBER UNION ALL 
SELECT 41 as NUMBER UNION ALL 
SELECT 42 as NUMBER UNION ALL 
SELECT 43 as NUMBER UNION ALL 
SELECT 44 as NUMBER UNION ALL 
SELECT 45 as NUMBER UNION ALL 
SELECT 46 as NUMBER UNION ALL 
SELECT 47 as NUMBER UNION ALL 
SELECT 48 as NUMBER UNION ALL 
SELECT 49 as NUMBER UNION ALL 
SELECT 50 as NUMBER UNION ALL 
SELECT 51 as NUMBER UNION ALL 
SELECT 52 as NUMBER UNION ALL 
SELECT 53 as NUMBER UNION ALL 
SELECT 54 as NUMBER UNION ALL 
SELECT 55 as NUMBER UNION ALL 
SELECT 56 as NUMBER UNION ALL 
SELECT 57 as NUMBER UNION ALL 
SELECT 58 as NUMBER UNION ALL 
SELECT 59 as NUMBER UNION ALL 
SELECT 60 as NUMBER UNION ALL 
SELECT 61 as NUMBER UNION ALL 
SELECT 62 as NUMBER UNION ALL 
SELECT 63 as NUMBER UNION ALL 
SELECT 64 as NUMBER UNION ALL 
SELECT 65 as NUMBER UNION ALL 
SELECT 66 as NUMBER UNION ALL 
SELECT 67 as NUMBER UNION ALL 
SELECT 68 as NUMBER UNION ALL 
SELECT 69 as NUMBER UNION ALL 
SELECT 70 as NUMBER UNION ALL 
SELECT 71 as NUMBER UNION ALL 
SELECT 72 as NUMBER UNION ALL 
SELECT 73 as NUMBER UNION ALL 
SELECT 74 as NUMBER UNION ALL 
SELECT 75 as NUMBER UNION ALL 
SELECT 76 as NUMBER UNION ALL 
SELECT 77 as NUMBER UNION ALL 
SELECT 78 as NUMBER UNION ALL 
SELECT 79 as NUMBER UNION ALL 
SELECT 80 as NUMBER UNION ALL 
SELECT 81 as NUMBER UNION ALL 
SELECT 82 as NUMBER UNION ALL 
SELECT 83 as NUMBER UNION ALL 
SELECT 84 as NUMBER UNION ALL 
SELECT 85 as NUMBER UNION ALL 
SELECT 86 as NUMBER UNION ALL 
SELECT 87 as NUMBER UNION ALL 
SELECT 88 as NUMBER UNION ALL 
SELECT 89 as NUMBER           -- Best to extend this count to a larger number, like 300+ 
) AS NUMBER_LIST 
CROSS JOIN (SELECT 'C:\wwsoftware\Application Server\3.0.000\3.0.001\IAS 3.0 P01.zip' as PATH) as PATH_QUERY 
GROUP BY PATH_QUERY.PATH 
) as X 

它很粗糙,因为它是一种强力方法,它需要适应允许的最大路径长度(由您的应用程序和the maximum path length of the OS)。

鉴于SQL Server Compact是一个嵌入式数据库(而不是客户端服务器数据库),因此没有理由不像ErikEJ建议的那样在应用程序代码中进行某些数据处理。这并不是说你减少了通过网络传输的结果集的大小。