472,780 Members | 1,169 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 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 5710
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>...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.