Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

InStr in SQL - MS Excel Query

Status
Not open for further replies.

barkley1979

Technical User
Jun 24, 2003
11
GB
I am not sure if you can help me here, but I cannot find anywhere else that can help me. (If anyone knows of a forum more suited to this question I would appreciate it!)

I need to create the following in SQL so that a query I am running will pull the right data into Excel for me.

I need to basically
SELECT * WHERE WO.WOTYPE is not in WO.TASKNUM
A typical example of the data is:
WOTYPE TASKNUM
CATR RM-CATR-EQUIP
CATR RM-CHG-GENERAL

Obviously I'd only want the second line showing in my results.

Can anyone assist?
Thanks.
 
Try either of the following:

select *
from wo
where wotype not like '*' & tasknum & '*';

or,

select *
from wo
where not instr(tasknum,wotype);

(not sure about the 2nd)

Good luck,
Dan
 
Sorry,

I think tasknum and wotype should be the other way round in my first query...
 
Can I restate this as

You want all records where the value in the "WOTYPE" field is NOT a substring of the value in "TASKNUM"?

If that's correct then try
Code:
   Select * From tbl 
   Where Instr(1,TASKNUM,WOTYPE) = 0
 
Thanks for both the replies.
With:

select *
from wo
where tasknum not like '*' & wotype & '*';

I get 'Invalid operator for data type. Operator equals boolean AND, type equals varchar. Statements could not be prepared'.

For:

Select * From tbl
Where Instr(1,TASKNUM,WOTYPE) = 0

I get 'Instr is not a recognized function name.'

As I'm new to all this I haven't a clue what's going on here!

Thanks again for the time.
 
"Instr" is a function available in MS/Access that locates the position of one string within another. If your system doesn't know about it then, are you using Access?

Try this (adapted from DanChard's code) which is standard SQL and should work on any RDBMS.

SELECT A.* FROM tbl As A
WHERE NOT (A.TASKNUM LIKE '*' + A.WOTYPE + '*' )
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top