|
Posted by Terry Kreft on 09/30/09 11:32
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.googlegroups.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.
>
[Back to original message]
|