March 25, 2013 at 12:24 am
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
March 25, 2013 at 12:40 am
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));
March 25, 2013 at 12:46 am
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
March 25, 2013 at 10:26 am
That's an awful lot like one of my favorite expressions in SSIS... lol
March 25, 2013 at 10:52 am
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)
March 25, 2013 at 10:54 am
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.
March 25, 2013 at 11:00 am
Agreed, I am just being lazy :p
March 25, 2013 at 11:12 am
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)
March 25, 2013 at 12:31 pm
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 25, 2013 at 3:46 pm
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) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy