简体   繁体   中英

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.

For example: 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

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

Try this one. Dono whether this a better solution to ur problem!! but i tried to reduce the usage of String functions then your's.

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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