Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Remove characters to the right of the last '\'

Status
Not open for further replies.

bmacbmac

IS-IT--Management
Jan 26, 2006
392
US
Hello,

I have the following data:

c:\images\0111 - 0114\simple.txt
c:\images\0222-0444\simple.txtc:\images\0333ab-4321ab\simple.txt

I would like to remove everything to the right of the last '\'. So I end up with:

c:\images\0111 - 0114c:\images\0222-0444c:\images\0333ab-4321ab
Can someone help me out with a query to do this?
 
One method:

Code:
DECLARE @str VARCHAR(1000)

SET @str = 'c:\images\0222-0444\simple.txt[URL unfurl="true"]http://www.tek-tips.com/threadminder.cfm?pid=183'[/URL]

SELECT
	@str 'Original',
	REVERSE(@str) 'Reverse',
	CHARINDEX('\', REVERSE(@str)) 'Index',
	SUBSTRING(REVERSE(@str), CHARINDEX('\', REVERSE(@str)), 1000) 'Substring',
	REVERSE(SUBSTRING(REVERSE(@str), CHARINDEX('\', REVERSE(@str)), 1000)) 'Final'

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
C#.NET Programmer
 
Thanks mstrmage1768. Worked perfectly. Your string of examples were also helpful and let me see the progression of the script.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top