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


Find last occurence of a character within a string


Find last occurence of a character within a string

Author
Message
ramadesai108
ramadesai108
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4093 Visits: 690
Hi All,
I am trying to find the last occurence of a character within a string. I have the following code: what is the easiest way to accomplish this?

SELECT CASE WHEN FieldA IS NULL THEN '' ELSE

set @pos = charindex('\',FieldA,@pos)

while(@pos)>0
begin
set @lastpos=@pos
set @pos=charindex('\',FieldA,@pos+1)
end

SUBSTRING(FieldA ,@pos + 1, LEN(FieldA) - (@pos + 1)) END AS FileUploaded
FROM TableA

Example data of FieldA : "DirectoryA\DirectoryB\DirectoryC\FileName"
"DirectoryA\DirectoryB\FileName"

Thank you.
KenSimmons
KenSimmons
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3524 Visits: 2614
You can use the Reverse Function to find the last occurrance.


Declare @FieldA varchar(100)
Set @FieldA = 'DirectoryA\DirectoryB\DirectoryC\FileName'

Select Substring(@FieldA,
LEN(@FieldA) -
Charindex('\',Reverse(@FieldA))+2,
LEN(@FieldA))

Ken Simmons
http://twitter.com/KenSimmons
GilaMonster
GilaMonster
SSC Guru
SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)

Group: General Forum Members
Points: 370950 Visits: 46965
Quickest way is to reverse the string and look for the first occurrence.

DECLARE @StringA varchar(100), @pos int
SET @stringA = 'DirectoryA\DirectoryB\FileName'

-- Where is the last '\'?

SET @pos = LEN(@StringA) - CHARINDEX('\',REVERSE(@StringA))
SELECT @pos



Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Ramesh Saive
Ramesh Saive
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10230 Visits: 2643
Or this way too...


DECLARE @SomeFile VARCHAR(255)

SET @SomeFile = 'C:\WINDOWS\system32\FileName.xls'

SELECT RIGHT( @SomeFile, CHARINDEX( '\', REVERSE( @SomeFile ) + '\' ) - 1 )



--Ramesh


Vijaya Kadiyala
Vijaya Kadiyala
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4119 Visits: 409
Ramesh (4/8/2009)
Or this way too...


DECLARE @SomeFile VARCHAR(255)

SET @SomeFile = 'C:\WINDOWS\system32\FileName.xls'

SELECT RIGHT( @SomeFile, CHARINDEX( '\', REVERSE( @SomeFile ) + '\' ) - 1 )



Perfect solution..with simple trick using REVERSE...

Thanks -- Vijaya Kadiyala
www.dotnetvj.com
SQL Server Articles For Beginers



andrewd.smith
andrewd.smith
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3638 Visits: 3232
If you're looking to retrieve the filename from a path, then this seems to do the job:

SELECT FieldA, COALESCE(RIGHT(FieldA, NULLIF(CHARINDEX('\', REVERSE(FieldA)) - 1, -1)), FieldA, '') AS Filename
FROM (
SELECT 'DirectoryA\DirectoryB\DirectoryC\FileName1' AS FieldA UNION ALL
SELECT 'DirectoryA\DirectoryB\FileName2' UNION ALL
SELECT 'FileName3' UNION ALL
SELECT 'DirectoryA\DirectoryB\' UNION ALL
SELECT '' UNION ALL
SELECT NULL
) TableA


ramadesai108
ramadesai108
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4093 Visits: 690
Thank you Ken Simmons, that worked. Have a nice day.
David Branscome
David Branscome
SSC Veteran
SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)

Group: General Forum Members
Points: 228 Visits: 688
Nice little bit of Code, Thanks!



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