A SELECT statement that retrieves data from all records by comparing a part of a field?

Hi:
I have a salary legacy system, which was designed in some strange way (it was adabas database/ non-relational database).
I import tables to SQL database server, so the table  tblMonthlyTransaction has a column named trnsDeductionCodes contains all the deduction codes for a specific person combined together.
So person No# 1 has these deduction codes” 545801702903904321” which really means that he has deduction codes No# 545, 801, 702, 903, 904 and 321. Three digits for each deduction code.
And remember that the code No# “545” some months appears in the beginning of the field, and some times not.
so please;
Which select statement should I have to use to retrieve only the people that have particular deduction code at specific month “transactionMonth”.
I have 8 million records in that table “tblMonthlyTransaction”, and I need to have only those people having deduction code No# 545 only, and for month No# “012013”.
Help me please.

I have some sample data to illustrate tables and data structure. See attached excel sheet please.salaryQuestion.xlsx
Mohammad Alsolaimanapplication programmerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Duy PhamFreelance IT ConsultantCommented:
To find only people having deduction code No# 545 only, and for month No# “012013”:

SELECT * FROM tblMonthlyTransaction WHERE transactionMonth = '012013' AND (CHARINDEX('545', trnsDeductionCodes) % 3 = 1)

Open in new window

PortletPaulEE Topic AdvisorCommented:
Unless you can divide up that field into further, normalized, rows that you store, I fear you are doomed to table scans of those 8 million rows.

AND (CHARINDEX('545', trnsDeductionCodes) % 3 = 1

is clever, and will permit you to exactly match to code only starting at the correct points in the string (assuming it is a string)

However it is not a "sargable predicate", i.e. it cannot make use of an index.

What is the MAXIMUM allowed width of that column? i.e. how is that column defined?
 ( e.g. varchar(36) )

{+ edit}
This result:
| trnsId | transactionMonth | PersId | trnsDeductionCode |
|--------|------------------|--------|-------------------|
|      1 |            12013 |      1 |               545 |
|      1 |            12013 |      1 |               801 |
|      1 |            12013 |      1 |               702 |
|      1 |            12013 |      1 |               903 |
|      1 |            12013 |      1 |               904 |
|      1 |            12013 |      1 |               321 |
|      2 |            12013 |      2 |               702 |
|      3 |            12013 |      3 |               545 |
|      4 |            12013 |      4 |               801 |
|      4 |            12013 |      4 |               702 |
|      4 |            12013 |      4 |               545 |
|      5 |            12013 |      5 |               903 |

Open in new window


produced by this query:
select
      trnsId, transactionMonth, PersId, ca.trnsDeductionCode
from tblMonthlyTransaction
cross apply (
  values
         ( substring(trnsDeductionCodes, 1, 3) )
       , ( substring(trnsDeductionCodes, 4, 3) )
       , ( substring(trnsDeductionCodes, 7, 3) )
       , ( substring(trnsDeductionCodes, 10, 3) )
       , ( substring(trnsDeductionCodes, 13, 3) )
       , ( substring(trnsDeductionCodes, 16, 3) )
       , ( substring(trnsDeductionCodes, 19, 3) )
       , ( substring(trnsDeductionCodes, 22, 3) )
       , ( substring(trnsDeductionCodes, 25, 3) )
       , ( substring(trnsDeductionCodes, 28, 3) )
       , ( substring(trnsDeductionCodes, 31, 3) )
       , ( substring(trnsDeductionCodes, 33, 3) )
   ) ca (trnsDeductionCode )
where ca.trnsDeductionCode > ''

Open in new window


see it working here http://sqlfiddle.com/#!6/99b47/3

ddl
CREATE TABLE tblDeduction
    ([deductionId] int, [PersId] int, [deductionCode] int, [deductionStatus] int, [deductionStartDate] datetime, [deductionEndDate] datetime, [deductionApproved] int, [deductionAmount] int)
;
    
INSERT INTO tblDeduction
    ([deductionId], [PersId], [deductionCode], [deductionStatus], [deductionStartDate], [deductionEndDate], [deductionApproved], [deductionAmount])
VALUES
    (1, 1, 545, 0, '2000-02-03 00:00:00', '2020-02-03 00:00:00', 0, 150),
    (2, 1, 801, 0, '2000-02-03 00:00:00', '2020-02-03 00:00:00', 0, 150),
    (3, 2, 702, 0, '2000-02-03 00:00:00', '2020-02-03 00:00:00', 0, 200),
    (4, 3, 545, 1, '2000-02-03 00:00:00', '2020-02-03 00:00:00', 0, 150),
    (5, 4, 801, 0, '2000-02-03 00:00:00', '2020-02-03 00:00:00', 0, 150),
    (6, 4, 702, 0, '2000-02-03 00:00:00', '2020-02-03 00:00:00', 0, 200),
    (7, 4, 545, 0, '2000-02-03 00:00:00', '2020-02-03 00:00:00', 0, 150),
    (8, 5, 903, 0, '2000-02-03 00:00:00', '2020-02-03 00:00:00', 0, 56),
    (9, 1, 702, 0, '2000-02-03 00:00:00', '2020-02-03 00:00:00', 0, 200),
    (10, 1, 903, 0, '2000-02-03 00:00:00', '2020-02-03 00:00:00', 0, 56),
    (11, 1, 904, 0, '2000-02-03 00:00:00', '2020-02-03 00:00:00', 0, 30),
    (12, 1, 321, 0, '2000-02-03 00:00:00', '2020-02-03 00:00:00', 0, 120)
;


CREATE TABLE tblMonthlyTransaction
    ([trnsId] int, [transactionMonth] int, [PersId] int, [trnsDeductionCodes] varchar(36))
;
    
INSERT INTO tblMonthlyTransaction
    ([trnsId], [transactionMonth], [PersId], [trnsDeductionCodes])
VALUES
    (1, 012013, 1, '545801702903904321'),
    (2, 012013, 2, '702'),
    (3, 012013, 3, '545'),
    (4, 012013, 4, '801702545'),
    (5, 012013, 5, '903')
;

Open in new window


but this is most useful if you store those extra rows and index the code column

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mohammad Alsolaimanapplication programmerAuthor Commented:
Thank you all for participating. And you are awesome Paul Maxwell.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.