Wednesday, March 7, 2012

How to find image name in URL?

If I have a string field with a URL, how can I pull out the image name?
Here are some examples and what I'd like returned:
http://somewebsite.com/myimage.gif
Return "image.gif"
http://somewebsite.com
Return "somewebsite.com"
http://somewebsite.com/89sdmksdfds9...kkkl.sldkfj.gif
Return "89sdmksdfds990s0s0s0kkkl.sldkfj.gif" (notice there are two dots
here)
http://somewebsite.com/89sdmksdfds990s0s0s0kkkl
Return "89sdmksdfds990s0s0s0kkkl"
Thanks,
BrettUse the RIGHT function to extract the right most portion of a string. Use
CHARINDEX function to find the first position of '/' in the string (reversed
in your case). Use REVERSE function to reverse a string.
Using all the above, you can do:
SELECT RIGHT( @.s, CHARINDEX('/', REVERSE( @.s ) ) - 1 )
Details & syntax of all the above mentioned functions can be found in SQL
Server Books Online.
Anith|||The OP might need to make minor adjustments for cases like:
http://www.foo.com/
http://www.foo.com/foo.gif/
Please post DDL, sample data and desired results.
See http://www.aspfaq.com/5006 for info.
"Anith Sen" <anith@.bizdatasolutions.com> wrote in message
news:OM86YE#KFHA.904@.tk2msftngp13.phx.gbl...
> Use the RIGHT function to extract the right most portion of a string. Use
> CHARINDEX function to find the first position of '/' in the string
(reversed
> in your case). Use REVERSE function to reverse a string.
> Using all the above, you can do:
> SELECT RIGHT( @.s, CHARINDEX('/', REVERSE( @.s ) ) - 1 )
> Details & syntax of all the above mentioned functions can be found in SQL
> Server Books Online.
> --
> Anith
>

No comments:

Post a Comment