Click here to Skip to main content
15,793,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to remove last part of word form string in sql server.

For E.g this is string in one column "Viewer\DWG MPP msg EML\New folder\1343939490.pdf"
i need to get word upto "Viewer\DWG MPP msg EML\New folder" and omit\remove last part form this word "\1343939490.pdf" can use split.i mean need to remove last slash and file name(\1343939490.pdf)

Note :i am going to use this query in where condition as sub i don't want in store procedure and that slash("\") also increase or decrease i mean string sometime contains

Viewer\DWG MPP msg EML\1343939490.pdf
Viewer\DWG MPP msg EML\New folder\DWG MPP msg EML\New folder\1343939490.pdf

Updated 6-Jul-23 23:11pm

1 solution

SQL doesn't have a Split function (it has no concept of arrays, so it can't return a collection of values except as a table).

However, it does have a Reverse function:
SELECT LEFT(Location, LEN(Location) - CHARINDEX('\', REVERSE('\' + Location))) AS [Result] from MyTable

You can always create an SQL Function to do that if you want to be readable in queries.
Share this answer
Maciej Los 27-Dec-14 10:48am    
Aravindba 28-Dec-14 6:20am    
Thanks a lot,+5
OriginalGriff 28-Dec-14 6:31am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900