1

I have a table in SQL Server 2012 that holds a list of parts, location of the parts and the quantity on hand. The problem I have is someone put a space in front of the location when they added it to the database. This allowed there to be two records.

I need to create a job that will find the parts with spaces before the location and add those parts to the identical parts without spaces in front of the location. I'm not quite sure where to even start with this.

This is the before:

  Partno  |  PartRev  |  Location  |  OnHand  |  Identity_Column
--------------------------------------------------------------------
  0D6591D    000          MV3         55.000     103939
  0D6591D    000         MV3          -55.000    104618

This is what I would like to have after the job ran:

  Partno  |  PartRev  |  Location  |  OnHand  |  Identity_Column
--------------------------------------------------------------------
  0D6591D    000         MV3         0           104618
4
  • Is it just a single white space ? or it can be more than just a single white space Commented Feb 1, 2017 at 15:56
  • Is the "after" row Identity_Column value a mistake? If you're keeping the row without a space it should be 104618, unless something else is going on... Commented Feb 1, 2017 at 16:00
  • Sorry Chris that was a mistake Commented Feb 1, 2017 at 16:03
  • Prdp it could potentially be more than a single space. Commented Feb 1, 2017 at 16:04

2 Answers 2

2

Two steps: 1. update the records with the correct locations, 2. delete the records with the wrong locations.

update mytable
set onhand = onhand + 
(
  select coalesce(sum(wrong.onhand), 0)
  from mytable wrong
  where wrong.location like ' %'
  and trim(wrong.location) = mytable.location
)
where location not like ' %';

delete from mytable where location like ' %';
Sign up to request clarification or add additional context in comments.

5 Comments

Duplicate keys? We are not inserting data, we are not updating keys, we are even deleting records. How can we get such an error then?
You may just want to change trim with ltrim or rtrim accordingly
The key is a combination of the Part, PartRev, and location by updating the location it creates a duplicate key error since those three would be exactly the same.
@TECC how does updating onHand cause duplicate key error?
Stephen you are totally correct I read the steps and not the code. I apologize for not reading your answer thoroughly.
1

You can do some grouping with a HAVING clause on to identify the records. I've used REPLACE to replace spaces with empty strings in the location column, you could also use LTRIM and RTRIM:

CREATE TABLE #Sample
    (
      [Partno] VARCHAR(7) ,
      [PartRev] INT ,
      [Location] VARCHAR(5) ,
      [OnHand] INT ,
      [Identity_Column] INT
    );


INSERT INTO #Sample
    ([Partno], [PartRev], [Location], [OnHand], [Identity_Column])
VALUES
    ('0D6591D', 000, ' MV3', 55.000, 103939),
    ('0D6591D', 000, 'MV3', -55.000, 104618)
;

SELECT Partno ,
       PartRev ,
       REPLACE( Location, ' ', '') Location,
       SUM(OnHand) [OnHand]
FROM #Sample
GROUP BY REPLACE(Location, ' ', '') ,
         Partno ,
         PartRev
HAVING COUNT(Identity_Column) > 1;

DROP TABLE #Sample;

Produces:

Partno  PartRev Location    OnHand
0D6591D 0       MV3         0

1 Comment

I think my biggest problem is finding the bad record and finding the matching good record.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.