July 16, 2010 at 11:36 am
Hello Everyone
I am not able to figure this seemingly very small issue out by myself.
I am trying to return the remainder of a string using SUBSTRING method. Nothing more than a complete path along with a file name at the end. All that I would like to have is the complete path, without the file name
example:
J:\SQL 2008 Database Backups\High School Baseball\HighSchoolBaseball_Full.bak
I want only this returned:
J:\SQL 2008 Database Backups\High School Baseball\
But I am not able to figure out using T-SQL how to accomplish this. I am very close, I just cannot get how to find the first '\' when starting at the right.
Thank you in advance
Andrew SQLDBA
July 16, 2010 at 11:42 am
Try this
DECLARE @strVariable VARCHAR(100)
SET @strVariable = 'J:\SQL 2008 Database Backups\High School Baseball\HighSchoolBaseball_Full.bak'
SELECTSUBSTRING( @strVariable, 1, LEN( @strVariable ) - CHARINDEX( '\', REVERSE( @strVariable ) ) )
Here i have used the REVERSE function to get the length from the right. Didnt test it though as i don't have access to SSMS right now
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
July 16, 2010 at 2:49 pm
Kingston,
It is helpful for me also.. thanks
🙂
July 16, 2010 at 3:00 pm
Kingston Dhasian (7/16/2010)
Try this
DECLARE @strVariable VARCHAR(100)
SET @strVariable = 'J:\SQL 2008 Database Backups\High School Baseball\HighSchoolBaseball_Full.bak'
SELECTSUBSTRING( @strVariable, 1, LEN( @strVariable ) - CHARINDEX( '\', REVERSE( @strVariable ) ) )
Here i have used the REVERSE function to get the length from the right. Didnt test it though as i don't have access to SSMS right now
Nicely done, Kingston. A lot of folks end up doing more than 1 Reverse trying to do the same thing.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 17, 2010 at 2:01 am
Jeff Moden (7/16/2010)
Kingston Dhasian (7/16/2010)
Try this
DECLARE @strVariable VARCHAR(100)
SET @strVariable = 'J:\SQL 2008 Database Backups\High School Baseball\HighSchoolBaseball_Full.bak'
SELECTSUBSTRING( @strVariable, 1, LEN( @strVariable ) - CHARINDEX( '\', REVERSE( @strVariable ) ) )
Here i have used the REVERSE function to get the length from the right. Didnt test it though as i don't have access to SSMS right now
Nicely done, Kingston. A lot of folks end up doing more than 1 Reverse trying to do the same thing.
Thanks for the compliment, Jeff. Just a few days back i had to do a similar job and i wasn't sure if this is a good way to do it. Now i am sure it is the best way. 🙂
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
July 17, 2010 at 6:19 am
A small modification, to allow for input strings that include one or more trailing spaces, and which will also work with a Unicode input string:
DECLARE @strVariable VARCHAR(100) = 'J:\SQL 2008 Database Backups\High School Baseball\HighSchoolBaseball_Full.bak';
SELECT LEFT(@strVariable, 1 + (DATALENGTH(@strVariable)/DATALENGTH(LEFT(@strVariable,1))) - CHARINDEX( '\', REVERSE(@strVariable)));
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
July 18, 2010 at 12:05 pm
Agreed... trailing spaces could come into play (and I always forget about them). Nicely done.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 18, 2010 at 10:57 pm
Paul White NZ (7/17/2010)
A small modification, to allow for input strings that include one or more trailing spaces, and which will also work with a Unicode input string:
DECLARE @strVariable VARCHAR(100) = 'J:\SQL 2008 Database Backups\High School Baseball\HighSchoolBaseball_Full.bak';
SELECT LEFT(@strVariable, 1 + (DATALENGTH(@strVariable)/DATALENGTH(LEFT(@strVariable,1))) - CHARINDEX( '\', REVERSE(@strVariable)));
Well, it didn't even cross my mind that trailing spaces will create problems. Thanks for pointing it out, Paul. Now i will be careful with strings and spaces.
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
July 19, 2010 at 12:26 am
All good, Kingston (and Jeff). It was a fairly small refinement to the original excellent answer.
Avoiding the extra REVERSE is neat.
My main reason for posting was to show off the trick to make it work with Unicode and ANSI character sets - the trailing spaces thing was an excuse 🙂
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
July 19, 2010 at 10:06 pm
Joe Celko (7/19/2010)
Watch out forSET @strVariable = 'J:\SQL 2008 Database Backups\High School Baseball\HighSchoolBaseball_Full.bak\'
The final slash messes up things.
True - though I wonder if that example represents a truly valid path...
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply