Google Answers Logo
View Question
 
Q: SQL Query, Stripping data in a field. ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: SQL Query, Stripping data in a field.
Category: Computers > Programming
Asked by: apcs_uk-ga
List Price: $10.00
Posted: 12 Dec 2003 03:06 PST
Expires: 11 Jan 2004 03:06 PST
Question ID: 286283
Hi,

I have a list or URLs in a column (field name URLs).

http://www.domain.com
http://1.domain.com
http://shop.domain.com
shopping.domain.com

etc. I want to bring these urls into an asp list page and order
alphabeticaly. Which means that, where applicable, i need to strip the
http:// and www. out. How can i do this with a query?

Also, if i have a field with a first and surname, how can i return
just the first or second name in a query. I think i need to use
regular expressions but i have never used them before and they look
kinda scary :)

Many thanks

Request for Question Clarification by joseleon-ga on 12 Dec 2003 06:17 PST
Hello, apcs_uk:
  In which server are you going to run the query? Access, SQL Server,
MySQL, Oracle... The query can vary depending on this.

Regards.

Clarification of Question by apcs_uk-ga on 12 Dec 2003 06:18 PST
MS access 2000

Request for Question Clarification by joseleon-ga on 12 Dec 2003 07:24 PST
Hello, apcs_uk:
  I have been looking for info about how to apply regular expressions
in Access queries, but I haven't found anything, maybe another
researcher could help you.

By the way, I think it would be easier and the code will be better if
you process the URLs field in ASP to extract the domain once you have
all the database records, this way will work with any kind of
database.

Regards.

Clarification of Question by apcs_uk-ga on 12 Dec 2003 07:26 PST
Hello there,

Can you provide some asp code to accomplish this?

Many thanks

Request for Question Clarification by joseleon-ga on 12 Dec 2003 08:26 PST
Hello, apcs_uk:
  Sorry, but one more request. Do you want asp code to strip the
domain from a URL or the full source code that reads the database,
strips the domain and dumps out the results?

Regards.
Answer  
Subject: Re: SQL Query, Stripping data in a field.
Answered By: joseleon-ga on 13 Dec 2003 01:48 PST
Rated:5 out of 5 stars
 
Hello, apcs_uk:

Here you have a sample ASP scripts that shows you how to extract urls
from a text and then extract all the pieces that make up the URLs,
including the domain name, the part you are interested in:

<%@ Language=VBScript %>
<%
	Option Explicit

	'Declare our variable
	Dim objRegExpr

	'Create an instance of the regexp object
	Set objRegExpr = New regexp


	objRegExpr.Pattern =
"(http:\/\/|https:\/\/|ftp:\/\/|)(([A-Z0-9][A-Z0-9_-]*\.)(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)))"
	
	objRegExpr.Global = True
	objRegExpr.IgnoreCase = True

	'What string are we searching on?
	Dim strSearchOn
	strSearchOn = "You can searc  http://www.domain.com for URLs in any text " & _
		"string http://1.domain.com/ and http://shop.domain.com/ extract all
that matches the regular shopping.domain.com expression"

	'Declare a variable to hold our collection of Matches
	Dim colMatches

	'Now, Execute the regular expression search
	Set colMatches = objRegExpr.Execute(strSearchOn)

	Dim objMatch

	'Print the # of matches we found
	Response.Write colMatches.Count & " matches found...<P>"

	'Step through our matches
	For Each objMatch in colMatches
		Response.Write "<HR>"		
		Response.Write "Full URL:&nbsp;&nbsp;" & objMatch.Value & "<BR>"
		Response.Write "Protocol:&nbsp;&nbsp;" & objMatch.SubMatches(0) & "<BR>"
		Response.Write "URL w/o domain:&nbsp;&nbsp;" & objMatch.SubMatches(1) & "<BR>"
		Response.Write "First part:&nbsp;&nbsp;" & objMatch.SubMatches(2) & "<BR>"
		Response.Write "Domain name:&nbsp;&nbsp;" & objMatch.SubMatches(3) & "<BR>"
						
	Next

	'Clean up
	Set colMatches = Nothing
	Set objRegExpr = Nothing
%>

Be aware of the word wrapping when copying and pasting.

This script produces the following results:

4 matches found...


--------------------------------------------------------------------------------
Full URL:  http://www.domain.com
Protocol:  http://
URL w/o domain:  www.domain.com
First part:  www.
Domain name:  domain.com

--------------------------------------------------------------------------------
Full URL:  http://1.domain.com
Protocol:  http://
URL w/o domain:  1.domain.com
First part:  1.
Domain name:  domain.com

--------------------------------------------------------------------------------
Full URL:  http://shop.domain.com
Protocol:  http://
URL w/o domain:  shop.domain.com
First part:  shop.
Domain name:  domain.com

--------------------------------------------------------------------------------
Full URL:  shopping.domain.com
Protocol:  
URL w/o domain:  shopping.domain.com
First part:  shopping.
Domain name:  domain.com

I hope this is what you were looking for, don't hesitate to request
for any clarification.

You can get a lot of information about regular expressions applied to
asp in this page:

Regular Expression Information 
http://www.4guysfromrolla.com/webtech/regularexpressions.shtml

Regards.
apcs_uk-ga rated this answer:5 out of 5 stars and gave an additional tip of: $5.00
Fantastic, worked almost straight off. Also very well commented.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy