简体   繁体   English

如何以良好的性能改进查询以使用SQL Server拆分字符串

[英]How to improve my query with good performance to split a string with SQL Server

I can split a string from the table below but my query was too long. 我可以从下表中拆分一个字符串,但查询时间过长。 I think there could be another query to make it more professional with SQL Server. 我认为可能会有另一个查询,使它与SQL Server更加专业。

For example: D:\\Desktop\\English Club\\Haloween S\\S.jpg . 例如: D:\\Desktop\\English Club\\Haloween S\\S.jpg The result which must be the folder name before the image file , is as Halowwen S or Screenshots 结果必须是image file之前的image file夹名称,如Halowwen SScreenshots

CREATE TABLE path(
  pathlink nvarchar(300)
)
INSERT INTO path VALUES('D:\Desktop\English Club\Haloween S\S.jpg')
INSERT INTO path VALUES('C:\Users\Safari\Pictures\Screenshots\Rate.png')
INSERT INTO path VALUES('T:\Users\users\Documents\Emicsoft Studio\ent.gif')

My query: 我的查询:

SELECT RIGHT(LEFT(pathlink, CHARINDEX(REVERSE(LEFT(REVERSE(pathlink), CHARINDEX(('\'), REVERSE(pathlink)))), pathlink)-1),
        CHARINDEX(('\'), REVERSE(LEFT(pathlink, CHARINDEX(REVERSE(LEFT(REVERSE(pathlink), CHARINDEX(('\'), REVERSE(pathlink)))), pathlink)-1)))-1)
FROM path

SQLFIDDLE: http://sqlfiddle.com/#!6/22ecc/2 SQLFIDDLE: http ://sqlfiddle.com/#!6/22ecc/2

Try this one. 试试这个。 Dono whether this a better solution to ur problem!! Dono这是否是您的问题的更好解决方案! but i tried to reduce the usage of String functions then your's. 但是我试图减少String functions的使用,然后减少您的使用。

SELECT Reverse(LEFT(Substring(Reverse(pathlink), Charindex('\', Reverse(pathlink)) + 1, 
       Len(pathlink)), Charindex('\', Substring(Reverse(pathlink), Charindex('\', Reverse(pathlink)) + 1, Len(pathlink))) - 1))
FROM   path 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM