Unix Technical Forum

Splitting a filename out of a filepath

This is a discussion on Splitting a filename out of a filepath within the SQL Server forums, part of the Microsoft SQL Server category; --> I have to create a view where the filename is seperated from the path to the file. Examples of ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 02:58 PM
starritt@gmail.com
 
Posts: n/a
Default Splitting a filename out of a filepath


I have to create a view where the filename is seperated from the path
to the file. Examples of the data include:

m:\images\big\myimg.jpg
m:\images\medium\myimg.jpg
z:\media\images\highqual\myimg.jpg

Is there a function that will return the position in a string of the
last \ ? With this I can easily use substring or similar to pull and
push this stuff into the fields I want. If this function does not
exist how do I move forward?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 02:58 PM
ZeldorBlat
 
Posts: n/a
Default Re: Splitting a filename out of a filepath

I don't think they have a CHARINDEX that searches from the end of the
string. If you want to know the last position of a character within a
string, you can abuse the reverse() function like this:

len(filepath) - charindex('/', reverse(filepath)) + 1

That gives you the position of the last '/' in the string. Note that
this only works if you're searching for a single character (that is, it
won't work if you replace the '/' with some arbitrary string with
length > 1). Also, if there is no '/' in the string, this guy will
return len(filename) + 1 as opposed to the normal charindex() which
returns 0 if the needle isn't found.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 02:58 PM
starritt@gmail.com
 
Posts: n/a
Default Re: Splitting a filename out of a filepath

Thank you sir -- that should work nicely.

J

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 02:59 PM
Terry Kreft
 
Posts: n/a
Default Re: Splitting a filename out of a filepath

If you apply reverse to the first parameter in the charindex you could
search for multiple chars as well.

e.g.

declare
@search varchar(255),
@find varchar(255)

SELECT
@search = 'm:\images\big\myimg.jpg',
@find = '\'

SELECT
len(@search) - charindex(reverse(@find), reverse(@search)) + 1 ,
LEFT(@search, len(@search) - charindex(reverse(@find), reverse(@search)) +
1)

Returns
----------- -------------
14 m:\images\big\

declare
@search varchar(255),
@find varchar(255)

SELECT
@search = 'm:\images\big\myimg.jpg',
@find = 'images\'

SELECT
len(@search) - charindex(reverse(@find), reverse(@search)) + 1 ,
LEFT(@search, len(@search) - charindex(reverse(@find), reverse(@search)) +
1)

Returns

----------- --------
10 m:\images\


--
Terry Kreft



"ZeldorBlat" <zeldorblat@gmail.com> wrote in message
news:1132341746.949663.317500@g44g2000cwa.googlegr oups.com...
>I don't think they have a CHARINDEX that searches from the end of the
> string. If you want to know the last position of a character within a
> string, you can abuse the reverse() function like this:
>
> len(filepath) - charindex('/', reverse(filepath)) + 1
>
> That gives you the position of the last '/' in the string. Note that
> this only works if you're searching for a single character (that is, it
> won't work if you replace the '/' with some arbitrary string with
> length > 1). Also, if there is no '/' in the string, this guy will
> return len(filename) + 1 as opposed to the normal charindex() which
> returns 0 if the needle isn't found.
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 02:59 PM
ZeldorBlat
 
Posts: n/a
Default Re: Splitting a filename out of a filepath

>If you apply reverse to the first parameter in the charindex you could
>search for multiple chars as well.


Good point...I hadn't thought of that.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-29-2008, 02:59 PM
Terry Kreft
 
Posts: n/a
Default Re: Splitting a filename out of a filepath

Wll, I didn't think of using reverse in the frst place so your still ahead
of me on this one <g>.

--
Terry Kreft



"ZeldorBlat" <zeldorblat@gmail.com> wrote in message
news:1132586940.573948.135490@g14g2000cwa.googlegr oups.com...
> >If you apply reverse to the first parameter in the charindex you could
>>search for multiple chars as well.

>
> Good point...I hadn't thought of that.
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:24 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com