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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Extract part of a string 1

Status
Not open for further replies.

Andel

Programmer
Joined
Feb 15, 2001
Messages
366
Location
US
For example, I have the following string:

"H:\SQL\DATA\Customer_data.dat"
"O:\SQL\LOG\Customer_log.dat"

I only want to get the "Customer_data.dat" and "Customer_log.dat". In other words, I want to exclude the drive letter and directories.

Is there a way I can do that? Thanks in advance! Andel
andelbarroga@hotmail.com
 
I do not know if SQL Server has syntax like VB or not.
In VB you may use InStrRev() function to determine how many characters
from the beginning of the string to the last "\", then use the Right() function
to extract the string you need which has the length calculated by the Len() function.
For example:
strA = "D:\asdg\asdg\sadg\asdg\adsg\agds\adsg\adsg\agds\agds\whatever.ext"
strB = Right(strA, Len(strA) - InStrRev(strA, "\"))
The return value for strB is "whatever.ext"
 
In SQL Server you can use the REVERSE, CHARINDEX and SUBSTRING functions to get what you need. I would supply an example, but its too late in the day and my brain is fried. Robert Bradley
Got extra money lying around? Visit:
 
I got it... thanks for the clue Robert!

declare @filename varchar(30)
set @filename = 'H:\SQL\DATA\Customer_data.dat'
select reverse(substring(reverse(@filename), 1, charindex('\', reverse(@filename)) -1))

Andel
andelbarroga@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top