Solved

TSQL Syntax

Posted on 2012-03-14
2
278 Views
Last Modified: 2012-03-14
I need to delete row in table A (Price Code File, OEPRCFIL)when there is not a match in Table B (Item Master file, IMItmIdx). The field in OEPrcFil that contains the item number is Filler_0001. If Filler_0001 ONLY contained the item number, the TSL would be:

(I will first build a Select rather than a Delete and verify the records)

Select Filler_0001 From OEPrcFil
WHere Filler_0001 Not in (Select Item_No from IMItmIdx)

The above works with the provision that Filler_0001 contains only the Item number. The problem I have is that the Filler_0001 contains data in addition to the Item Number. Specifically, Filler_0001 contains the Vendor Number with the Item Number concatonated onto the end, e.g.  VENDORNOITEMNO = ACMEPRODUCTA or GIANTPRODUCTA.

As you can see the Vendor Number that prepends the Item Number is not a fixed length. Since it is not a fixed length, that preludes identifying the Item Number with Substring.

I need some sort of pattern matching. I have looked at CHARINDEX and PATINDEX but I was not successful in using a SELECT inside those functions.

I am using SQL 2000.

What TSQL syntax would I use to select all the OEPrdFil Records that do not have an item number match in the Item Master table?

Thanks,
pat
0
Comment
Question by:mpdillon
2 Comments
 
LVL 42

Accepted Solution

by:
dqmq earned 500 total points
ID: 37720587
This is not 100% logically correct, but perhaps it will steer you in a different direction.

Select Filler_0001 From OEPrcFil o, IMItmIdx m
where m.item_no as varchar(10) =   right(o.filler_0001,len(m.item_no)


What is the datatype of item_no?
do you also have a vendor table that would help construct the vendorproduct key.
0
 

Author Closing Comment

by:mpdillon
ID: 37720876
dqmq,

Great. With some modifications plus information I didn't share (didn't want to confuse you), I was able to solve the problem.

Yes, there is a Customer Master table. It was the "From OEPrcFil o, IMItmIdx M" that I was not familiar with.

Here is my solution

Select * from OEPrcFil_SQL
Where Cd_Tp = 1
and Ltrim(Rtrim(Filler_0001))
not in (Select ltrim(Rtrim(C.Cus_No))+ltrim(Rtrim(I.Item_No))
From IMItmIDX_SQL I, ARCusFil_SQL C)

The Delete query looks like:

Delete from OEPrcFil_SQL
Where Cd_Tp = 1
and Ltrim(Rtrim(Filler_0001))
not in (Select ltrim(Rtrim(C.Cus_No))+ltrim(Rtrim(I.Item_No))
From IMItmIDX_SQL I, ARCusFil_SQL C)


The SELECT is very slow (execution time > 1min) but I only have to do this twice so we will just live with it.

Thank you for your assistance.
pat
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question