SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Remove characters after last slash in string


Remove characters after last slash in string

Author
Message
SQLSACT
SQLSACT
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2102 Visits: 2931
Hi All

I have the following string
declare @string varchar (100)
set @string = 'x:\folder1\folder2\folder3\test.txt'
select @string



How would I remove all the characters after the last '\' in this string?

I need the string to reflect ''x:\folder1\folder2\folder3\'

Any Ideas?

Thanks
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39477 Visits: 38552
A bit expensive, but the following works:



declare @string varchar (100)
set @string = 'x:\folder1\folder2\folder3\test.txt'
select @string

select @string,reverse(right(reverse(@string), len(@string) - charindex('\',reverse(@string),1) + 1));




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
SQLSACT
SQLSACT
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2102 Visits: 2931
Lynn Pettis (3/25/2013)
A bit expensive, but the following works:



declare @string varchar (100)
set @string = 'x:\folder1\folder2\folder3\test.txt'
select @string

select @string,reverse(right(reverse(@string), len(@string) - charindex('\',reverse(@string),1) + 1));





Thank You
Erin Ramsay
Erin Ramsay
SSC Eights!
SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)

Group: General Forum Members
Points: 817 Visits: 1099
That's an awful lot like one of my favorite expressions in SSIS... lol
raym85
raym85
SSC-Addicted
SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)

Group: General Forum Members
Points: 402 Visits: 198
if you always know the len of the extnetion this should work as well without being so taxing.



declare @string varchar (100)
set @string = 'x:\folder1\folder2\folder3\test.txt'
select SUBSTRING(@string,1, LEN(@string)-8)
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39477 Visits: 38552
raym85 (3/25/2013)
if you always know the len of the extnetion this should work as well without being so taxing.



declare @string varchar (100)
set @string = 'x:\folder1\folder2\folder3\test.txt'
select SUBSTRING(@string,1, LEN(@string)-8)


Probably not a good assumption to make.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
raym85
raym85
SSC-Addicted
SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)

Group: General Forum Members
Points: 402 Visits: 198
Agreed, I am just being lazy :p
raym85
raym85
SSC-Addicted
SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)

Group: General Forum Members
Points: 402 Visits: 198
It bothered me now.

So i had to come up with a way to, but I would prolly choose the first one opver my own.


DECLARE @LastValue varchar(100)

declare @string varchar (100)
set @string = 'x:\folder1\folder2\folder3\test.txt'

SET @LastValue = ( CHARINDEX('\', REVERSE(@string)))



select SUBSTRING(@string,1, (LEN(@STRING) - CONVERT(INT,@LastValue))+1)
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26153 Visits: 17539
raym85 (3/25/2013)
It bothered me now.

So i had to come up with a way to, but I would prolly choose the first one opver my own.


DECLARE @LastValue varchar(100)

declare @string varchar (100)
set @string = 'x:\folder1\folder2\folder3\test.txt'

SET @LastValue = ( CHARINDEX('\', REVERSE(@string)))



select SUBSTRING(@string,1, (LEN(@STRING) - CONVERT(INT,@LastValue))+1)


This actually is pretty good. With a little modification you can remove the extra variable.


select SUBSTRING(@string,1, LEN(@STRING) - CHARINDEX('\', REVERSE(@string)) +1)



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
ScottPletcher
ScottPletcher
SSCertifiable
SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)

Group: General Forum Members
Points: 7911 Visits: 7155
And, finally, you don't really need SUBSTRING, LEFT is good enough :-):


SELECT @string, LEFT(@string, LEN(@string) - CHARINDEX('\', REVERSE(@string)) + 1)



SQL DBA,SQL Server MVP(07, 08, 09)[size=2]Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial: If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.[/size]
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search