473,396 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Using charindex AND substr to pull out data

I'm attempting to pull a list of Google search terms from url's stored in a database. Here is an example of the data in the table I'm dealing with

http://www.google.com/search?hl=en&q=proxy
http://news.google.com/news?hl=en&q=proxy&um=1&ie=UTF-8&imgefp=Q-gUBRtWxEsJ&imgurl=cache.boston.com/resize/bonzai-fba/Globe_Photo/2008/06/19/1213929665_4899/539w.jpg
http://www.google.com/search?hl=en&q=proxy

I have written a query that gets me halfway there:

select user_id, substring (FULL_URL, charindex ('q=', FULL_URL)+2,100) as Query, full_url
from LOG_DETAILS where FULL_URL != '' AND FULL_URL LIKE '%en&q=%' AND CATEGORY = '76'

The only issue I have is that this will pull everything right of q= giving me this

proxy
proxy&um=1&ie=UTF-8&imgefp=Q-gUBRtWxEsJ&imgurl=cache.boston.com/resize/bonzai-fba/Globe_Photo/2008/0
proxy


now I would like to also remove the trailing info after the &.

Any Help is much appreciated, it's been awhile since I've used transact SQL so explanations are helpful.

Thanks,
Joel
Aug 3 '08 #1
3 2417
Delerna
1,134 Expert 1GB
If I understand you correctly you need to
1) get the charindex of the first &
from the string to the right of that
2) get the charindex if the first &
If its zero choose the length of the string to the right of & in 1)
3) obtain the string between the 2 &'s

This is the basic idea, you can work out the details.
It can be done I have done it.
Ask if you have trouble, its a little mind boggling but not much.

I suggest tackling it bit by bit.
ie in a query return the whole string as a field
then add a field for the index of & no 1
then add a field for the string to the right of that
then add a field for the index of & no 2
then add a field that combines all of that to get the string you want
Aug 4 '08 #2
I got it, here's the final query. Hopefully you can get the drift of what I did. Reverse, Substring and Charindex were the keys.

Thanks,
Joel

create table # temp ( query char (100))
insert into #temp (query)
select select substring (URL, charindex ('q=', URL)+2,100)
from sometable where URL LIKE '%en&q=%'


update #temp set query = reverse(substring (reverse(query), charindex ('mu&', reverse(query))+3,100)) where query like '%&um%'
update #temp set query = reverse(substring (reverse(query), charindex ('ei&', reverse(query))+3,100)) where query like '%&ie%'
update #temp set query = reverse(substring (reverse(query), charindex ('rf&', reverse(query))+3,100)) where query like '%&fr%'
update #temp set query = reverse(substring (reverse(query), charindex ('ml&', reverse(query))+3,100)) where query like '%&lmt%'
update #temp set query = reverse(substring (reverse(query), charindex ('ts&', reverse(query))+3,100)) where query like '%&start%

select * from #temp
Aug 6 '08 #3
Delerna
1,134 Expert 1GB
Glad to see you worked out a solution. Not how I would have done it but you learn more from working out your own solution, and it works to your satisfaction, so.....
Aug 7 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: M Wells | last post by:
Hi All, Just wondering if it's any more efficient to use the following SQL statement to return a partial match from a column as opposed to the second statement. SELECT * FROM Table1 WHERE...
14
by: sdowney717 | last post by:
Using the the NumId from TitleData, I would like to delete the corresponding row in Bookdata using pure SQL. I want it to delete all rows in bookdata where the Titledata.NumID is a match to...
2
by: Little PussyCat | last post by:
Hello, I need to be able to replace only the first occurance of a space character in a column. Reason being is the data in the column I am trying to replace seems to have umpteen space...
5
by: Willem | last post by:
Hello I am quite hopeless and of course a newbe. The situation: Sql2k / query I would like it ot break down the following string: 2004 Inventory:Ex.Plant Farm1:1st Cut:Premium:0094
3
by: csomberg | last post by:
SQL Server 2000 Ya know, it is always the simplest stuff that gets ya !! I am having the hardest time getting a simple piece of code working. Must be brain dead today. Goal: Get the users...
11
by: youngster94 | last post by:
Hey all, I've written a VB.Net app that creates picture badges complete with barcodes. The problem is that the barcode quality is not good enough to be read by scanners. I'm using the...
2
by: ameshkin | last post by:
Hi GUys, Im trying to compare two dates in MYSQL. But its not treating the dates as numbers, but as strings. I try using strtotime but that did not work. Basically, if the last comment is...
0
by: anonieko | last post by:
This approach I found very efficient and FAST when compared to the rowcount, or Subquery Approaches. This is before the advent of a ranking function from DB such as ROW_NUMBER() in SQL Server...
2
by: matthewwhaley | last post by:
What is the best way to essentially use the charindex(find) function if the value is could be more than one variable (A or B or C) I can't seem to get an "or", "if" or "select if" to work ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.