473,385 Members | 1,324 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,385 software developers and data experts.

Help with a substring query

I need help capturing information from a free text field.
The 10 examples below contain examples of the information I am trying
to capture.
In each cell I am trying to capture the number between 'TranID=' and
the next '&'.
So in the fisrt cell I would like to capture 14078800.

Things you will need to keep in mind are;

The number is not of fixed length.
'TranID' will always precede the number
The number will always be followed by an '&'
The '&' sign can occur multiple times in the text.
Code=web.co.uk%product_250p&TranID=14078800&OtherF lag0=services.web.stats.PostContentDelivery&OtherF lag0par=deliv
Code=web.co.uk%product_free&TranID=14077576&OtherF lag0=services.web.stats.PostCSDelivery&OtherFlag0p ar=deliv
Code=web.co.uk%product_250p&TranID=14077583&OtherF lag0=services.web.stats.PostCSDelivery&OtherFlag0p ar=deliv
Code=web.co.uk%product_250p&TranID=14077584&OtherF lag0=services.web.stats.PostCSDelivery&OtherFlag0p ar=deliv
Code=web.co.uk%product_150p&TranID=14077579&OtherF lag0=services.web.stats.PostCSDelivery&OtherFlag0p ar=deliv
Code=web.co.uk%product_250p&TranID=14077603&OtherF lag0=services.web.stats.PostCSDelivery&OtherFlag0p ar=deliv
Code=web.co.uk%product_250p&TranID=14077741&OtherF lag0=services.web.stats.PostContentDelivery&OtherF lag0par=deliv
Code=web.co.uk%product_250p&TranID=14077757&OtherF lag0=services.web.stats.PostContentDelivery&OtherF lag0par=deliv
Code=web.co.uk%2Fpush_wallpaper_250p&TranID=140777 70&OtherFlag0=services.web.stats.PostContentDelive ry&OtherFlag0par=deliv
Code=web.co.uk%product_250p&TranID=14077604&OtherF lag0=services.web.stats.PostContentDelivery&OtherF lag0par=deliv
Regards,
Ciarán

Mar 20 '06 #1
8 5734
ch********@hotmail.com wrote:
I need help capturing information from a free text field.
The 10 examples below contain examples of the information I am trying
to capture.
In each cell I am trying to capture the number between 'TranID=' and
the next '&'.
So in the fisrt cell I would like to capture 14078800.

Things you will need to keep in mind are;

The number is not of fixed length.
'TranID' will always precede the number
The number will always be followed by an '&'
The '&' sign can occur multiple times in the text.
Code=web.co.uk%product_250p&TranID=14078800&OtherF lag0=services.web.stats.PostContentDelivery&OtherF lag0par=deliv
Code=web.co.uk%product_free&TranID=14077576&OtherF lag0=services.web.stats.PostCSDelivery&OtherFlag0p ar=deliv
Code=web.co.uk%product_250p&TranID=14077583&OtherF lag0=services.web.stats.PostCSDelivery&OtherFlag0p ar=deliv
Code=web.co.uk%product_250p&TranID=14077584&OtherF lag0=services.web.stats.PostCSDelivery&OtherFlag0p ar=deliv
Code=web.co.uk%product_150p&TranID=14077579&OtherF lag0=services.web.stats.PostCSDelivery&OtherFlag0p ar=deliv
Code=web.co.uk%product_250p&TranID=14077603&OtherF lag0=services.web.stats.PostCSDelivery&OtherFlag0p ar=deliv
Code=web.co.uk%product_250p&TranID=14077741&OtherF lag0=services.web.stats.PostContentDelivery&OtherF lag0par=deliv
Code=web.co.uk%product_250p&TranID=14077757&OtherF lag0=services.web.stats.PostContentDelivery&OtherF lag0par=deliv
Code=web.co.uk%2Fpush_wallpaper_250p&TranID=140777 70&OtherFlag0=services.web.stats.PostContentDelive ry&OtherFlag0par=deliv
Code=web.co.uk%product_250p&TranID=14077604&OtherF lag0=services.web.stats.PostContentDelivery&OtherF lag0par=deliv


Hint: use LIKE.

Kind regards

robert
Mar 20 '06 #2
--something like this:
declare @tranid_position int, @amp_position int,@string varchar(8000),
@rest_of_string varchar(8000), @Result_string varchar(8000)
set
@string='Code=web.co.uk%product_free&TranID=140775 76&OtherFlag0=services.web.stats.P*ostCSDelivery&O therFlag0par=deliv
'
set @tranid_position=charindex('&TranID=',@string)
set @rest_of_string= substring(@string,@tranid_position+8,8000)
set @amp_position=charindex('&',@rest_of_string)
set @Result_string=left(@rest_of_string,@amp_position-1)
select @Result_string

Mar 20 '06 #3
Here you go...

CREATE TABLE QueryStringTest (QueryString nvarchar(4000))
GO

INSERT INTO QueryStringTest
SELECT
'Code=web.co.uk%product_250p&TranID=14078800&Other Flag0=services.web.stats.PostContentDelivery&Other Flag0par=deliv'
UNION SELECT
'Code=web.co.uk%product_free&TranID=14077576&Other Flag0=services.web.stats.PostCSDelivery&OtherFlag0 par=deliv'
UNION SELECT
'Code=web.co.uk%product_250p&TranID=14077583&Other Flag0=services.web.stats.PostCSDelivery&OtherFlag0 par=deliv'
UNION SELECT
'Code=web.co.uk%product_250p&TranID=14077584&Other Flag0=services.web.stats.PostCSDelivery&OtherFlag0 par=deliv'
UNION SELECT
'Code=web.co.uk%product_150p&TranID=14077579&Other Flag0=services.web.stats.PostCSDelivery&OtherFlag0 par=deliv'
UNION SELECT
'Code=web.co.uk%product_250p&TranID=14077603&Other Flag0=services.web.stats.PostCSDelivery&OtherFlag0 par=deliv'
UNION SELECT
'Code=web.co.uk%product_250p&TranID=14077741&Other Flag0=services.web.stats.PostContentDelivery&Other Flag0par=deliv'
UNION SELECT
'Code=web.co.uk%product_250p&TranID=14077757&Other Flag0=services.web.stats.PostContentDelivery&Other Flag0par=deliv'
UNION SELECT
'Code=web.co.uk%2Fpush_wallpaper_250p&TranID=14077 770&OtherFlag0=services.web.stats.PostContentDeliv ery&OtherFlag0par=deliv'
UNION SELECT
'Code=web.co.uk%product_250p&TranID=14077604&Other Flag0=services.web.stats.PostContentDelivery&Other Flag0par=deliv'

SELECT
SUBSTRING(QueryString,
CHARINDEX(
'TRANID=',
UPPER(QueryString))+7,
CHARINDEX('&',
QueryString,
CHARINDEX(
'TRANID=',
UPPER(QueryString)
)
)-CHARINDEX(
'TRANID=',
UPPER(QueryString)
)-7
)
AS TransID
FROM QueryStringTest ORDER BY TransID
DROP TABLE QueryStringTest

You could also look into regular expressions.

Mar 20 '06 #4
That worked perfectly.
Much appreciated.

Mar 20 '06 #5
I've just encountered a little problam.
Contrary to the criteria I provided earlier there are cells which end
with the TranID, like for example

OtherID=1638256785230&TranID=12345

How do I edit the script to capture these records?

Regards,
Ciarán

Mar 20 '06 #6
There may be a better way but this should work...

CREATE TABLE QueryStringTest (QueryString nvarchar(4000))
GO

INSERT INTO QueryStringTest
SELECT
'Code=web.co.uk%product_250p&TranID=14078800&Other Flag0=services.web.stats.PostContentDelivery&Other Flag0par=deliv'
UNION SELECT
'Code=web.co.uk%product_free&TranID=14077576&Other Flag0=services.web.stats.PostCSDelivery&OtherFlag0 par=deliv'
UNION SELECT
'Code=web.co.uk%product_250p&TranID=14077583&Other Flag0=services.web.stats.PostCSDelivery&OtherFlag0 par=deliv'
UNION SELECT
'Code=web.co.uk%product_250p&TranID=14077584&Other Flag0=services.web.stats.PostCSDelivery&OtherFlag0 par=deliv'
UNION SELECT
'Code=web.co.uk%product_150p&TranID=14077579&Other Flag0=services.web.stats.PostCSDelivery&OtherFlag0 par=deliv'
UNION SELECT
'Code=web.co.uk%product_250p&TranID=14077603&Other Flag0=services.web.stats.PostCSDelivery&OtherFlag0 par=deliv'
UNION SELECT
'Code=web.co.uk%product_250p&TranID=14077741&Other Flag0=services.web.stats.PostContentDelivery&Other Flag0par=deliv'
UNION SELECT
'Code=web.co.uk%product_250p&TranID=14077757&Other Flag0=services.web.stats.PostContentDelivery&Other Flag0par=deliv'
UNION SELECT
'Code=web.co.uk%2Fpush_wallpaper_250p&TranID=14077 770&OtherFlag0=services.web.stats.PostContentDeliv ery&OtherFlag0par=deliv'
UNION SELECT
'Code=web.co.uk%product_250p&TranID=14077604&Other Flag0=services.web.stats.PostContentDelivery&Other Flag0par=deliv'
UNION SELECT
'OtherID=1638256785230&TranID=12345'

SELECT
CASE WHEN
CHARINDEX('&',
QueryString,
CHARINDEX(
'TRANID=',
UPPER(QueryString)
)
) = 0
THEN
RIGHT(QueryString,
LEN(QueryString)-
CHARINDEX(
'TRANID=',
UPPER(QueryString)
)-6
)
ELSE
SUBSTRING(QueryString,
CHARINDEX(
'TRANID=',
UPPER(QueryString))+7,
CHARINDEX('&',
QueryString,
CHARINDEX(
'TRANID=',
UPPER(QueryString)
)
)-CHARINDEX(
'TRANID=',
UPPER(QueryString)
)-7
)
END AS TransID
FROM QueryStringTest ORDER BY TransID

DROP TABLE QueryStringTest

Mar 20 '06 #7
(ch********@hotmail.com) writes:
I've just encountered a little problam.
Contrary to the criteria I provided earlier there are cells which end
with the TranID, like for example

OtherID=1638256785230&TranID=12345

How do I edit the script to capture these records?


Here is a query, a little different from figitals:

SELECT convert(int, str2)
FROM (SELECT str2 =
CASE WHEN str1 LIKE '%[^0-9]%'
THEN substring(str1, 1, patindex('%[^0-9]%', str1) - 1)
ELSE str1
END
FROM (SELECT str1 = substring(str,
charindex('TranID=', str) + len('TranId='),
len(str))
FROM QueryStringTest) AS a) AS b

By using nested derived tables, it is possibly easier to see the
solution step for step. Or it's more confusing. :-)
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Mar 20 '06 #8
Perfect!
Thanks

Mar 21 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Raj | last post by:
Hi all, Can anyone help me with a script which would delete files or move them to a different folder at some scheduled time..! Please.....!!! Thanks in advance...
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
4
by: techquest | last post by:
got slightly similar table. #Temp Table: name,name1,name2,exam,score A,A1,A21, A,A1,A21,math100,88 A,A1,A21,math101,56 A,A1,A21, A,A1,A21,math102,67 A,A1,A21, A,A1,A21,math104,45
7
by: Alan Bashy | last post by:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help...
28
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical...
0
by: jaggee | last post by:
Hello, This is regarding log analysis of a web system, I am finding my backend SQL programming has taken so much of time to process the application due to following quires for a log table having...
12
by: Amanda | last post by:
I have tried everything with this! I get an error stating "Index was outside the bounds of the array" My code looks like this.... xmlDoc = New XmlDocument() xmlDoc.Load("xml.doc") xslDoc...
0
by: RCapps | last post by:
When running the below SQL Query I keep getting the following error: Server: Msg 4924, Level 16, State 1, Line 1 ALTER TABLE DROP COLUMN failed because column 'ContractDef' does not exist in table...
6
by: zaina | last post by:
hi everybody i am nwebie in this forum but i think it is useful for me and the member are helpful my project is about connecting client with the server to start exchanging messages between...
4
by: Jonathan Wood | last post by:
I'm trying to duplicate an HTML sample I have using my ASP.NET pages. The sample contains the following within the <headtag: <script type="text/javascript" src="flashobject.js"></script>...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.