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

How do I put two Substring statements together into one?

Status
Not open for further replies.

JanetH

Technical User
Joined
Dec 31, 2003
Messages
6
Location
US
I have two substring queries that both work on their own but I can't seem to get the syntax right when I try and put them together into one query.
The first one is to remove the extension from a string
SUBSTRING(URI, 1, CHARINDEX('.',(URI)))

The second one is to return only the title of the page in a web address
REVERSE(SUBSTRING(REVERSE(URI), 1, CHARINDEX('/',REVERSE(URI))))

Whenever I try to put them together I get a syntax error or I get two seperate results, one without the extension and one containing just the title.
Thanks for your help.
 
What's is the value of your URI? And what would you like the output to be?
Andel
andelbarroga@hotmail.com
 
URI is the coloumn that I want to select so the value for it always changes depending on the record. An example of a record would be /wep/employee/Benefits.htm. What I would like to end up with is just Benefits.
With the first substring I gave above I would get
/wep/employee/Benefits.
The second would return:
Benefits.htm
So somehow I need to put the two together to just return Benefits.
 
This works, i've replace @URI with your @URI. the text below will run in sqlserver

declare @URI as varchar(255)
select @URI = '/wep/employee/Benefwwits.htm'

select
substring(REVERSE(SUBSTRING(REVERSE(@URI), 1, CHARINDEX('/',REVERSE(@URI))-1)),1, charindex('.',REVERSE(SUBSTRING(REVERSE(@URI), 1, CHARINDEX('/',REVERSE(@URI))-1)))-1)

 
If you only want to return "benefits", you can do just one select statement.

select reverse(substring(reverse(@url), charindex('.', reverse(@url)) + 1, charindex('/',reverse(@url)) - charindex('.', reverse(@url)) - 1))



Andel
andelbarroga@hotmail.com
 
Thank you both so much. It works like a dream!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top