472,791 Members | 1,806 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,791 software developers and data experts.

Is this query possible ?

Folks

Not sure how to tackle this in DB2...

I'd like to have a query that would check for the presence of a given
token in a text string and return the next "word".

Let's have Table1 with text field Field1:

Field1
---------------------------------------
This is a test !
Color blue or red !
Katrina is a CAT4 hurricane !

I'd like to search for "Color" and return the string "blue"
(the word delimiter being one or more space...

Any idea / suggestion welcome

Regards

--alexT

Nov 12 '05 #1
4 1127
------------------------- Commands Entered -------------------------
CREATE TABLE NextWord
(Field1 VARCHAR(30)
);
------------------------------------------------------------------------------
DB20000I The SQL command completed successfully.

------------------------- Commands Entered -------------------------
INSERT INTO NextWord
VALUES
('This is a test')
,('Color blue or red')
,('Katrina is a CAT4 hurricane')
,('Color blue or red')
,('My Car Color is blue')
;
------------------------------------------------------------------------------
DB20000I The SQL command completed successfully.

------------------------- Commands Entered -------------------------
SELECT Field1_Org
, SUBSTR(Field1,1,exist*(POSSTR(Field1,' ')-1)) Next_word
FROM (SELECT Field1
, LTRIM(SUBSTR(Field1,POSSTR(' '||Field1,' Color ')+6)) ||
' '
, SIGN(POSSTR(' ' || Field1,' Color '))
FROM NextWord
) Q(Field1_Org,Field1,exist);
------------------------------------------------------------------------------

FIELD1_ORG NEXT_WORD
------------------------------ -------------------------------
This is a test
Color blue or red blue
Katrina is a CAT4 hurricane
Color blue or red blue
My Car Color is blue is

5 record(s) selected.

Nov 12 '05 #2
Minor revised example:
SELECT Field1
, SUBSTR(Field2,1,SIGN(POSSTR(' '||Field1,' Color '))
*(POSSTR(Field2,' ')-1)) Next_word
FROM (SELECT Field1
, LTRIM(SUBSTR(Field1,POSSTR(' '||Field1,' Color ')+6)) ||
' '
FROM NextWord
) Q(Field1,Field2);

Nov 12 '05 #3
DECLARE GLOBAL TEMPORARY TABLE SESSION.NextWord(Field1 VARCHAR(30))

INSERT INTO SESSION.NextWord (Field1) \
VALUES \
('This is a test'), \
('Color blue or red'), \
('Katrina is a CAT4 hurricane'), \
('Color blue or red'), \
('My Car Color is blue'), \
('Blue is a very nice Color ')

SELECT \
SUBSTR \
( \
LTRIM(SUBSTR(Field1, LOCATE('Color ', Field1) + 6)), \
1, \
LOCATE \
( \
' ', \
LTRIM(SUBSTR(Field1, LOCATE('Color ', Field1) + 6)) \
) - 1 \
) \
FROM \
SESSION.NextWord \
WHERE \
LOCATE('Color ', Field1) > 0 \
AND LOCATE \
( \
' ', \
LTRIM(SUBSTR(Field1, LOCATE('Color ', Field1) + 6)) \
) - 1 > 0

DROP TABLE SESSION.NextWord

Of course, it'd be a lot easier if it wasn't 1 *or more* spaces.

B.

Nov 12 '05 #4
Wow - thanks !

I've learn something :)

Nov 12 '05 #5

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

Similar topics

3
by: Jonathan | last post by:
Hi all! For a match schedule I would like to find all possible combinations of teams playing home and away (without teams playing to themselves of course). I now the simple version works...
3
by: cassandra.flowers | last post by:
Hi, I was wondering if it is possible (Using access) to have a query parameter as a drop down box rather than a text box? e.g. typing as criteria for a query produces a box with a text box...
1
by: Michelle | last post by:
I have took another approach to my database and have created a field in my clients table with a 'client status' field, which is a combo box/value list that is a pulldown menu of 3 choices (active...
3
by: Ryan Hubbard | last post by:
I would like to run a query from VBA. The query will be one with parameters. Is there a way to run the query and have Access prompt for the values like it does if I where to execute it through...
3
by: Pea | last post by:
Is it possible to get the average in a query where I have multiple criteria? Here's an abbreviated example of the query in design view: Fields: USERID DATE TIME ID_TYPE...
4
by: d.p. | last post by:
Hi all, I'm using MS Access 2003. Bare with me on this description....here's the situation: Imagine insurance, and working out premiums for different insured properties. The rates for calculating...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
2
by: jim Bob | last post by:
Hi, I have a query with the criteria of (!..) Or !.. When I provide the value from qmylist, a second pop up appears waiting for the value from lstResults. If I enter nothing and click ok, it...
13
by: forbes | last post by:
Hi, I have a user that used the Query Wizard to create a query in Access. Now she claims that her master table is missing all the data that was excluded from the query. Can you create anything...
5
by: jonm4102 | last post by:
I'm trying to calculate the median of some numerical data. The data can only be found in a query (henceforth query 1) field I previously made, and I would prefer to calculate the median in a new...
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
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 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: 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:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
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.