(SELECT DISTINCT [LOT]
FROM WIPTEST
WHERE
[LOT] LIKE [Component]+'%')
create table junk (component varchar(10), lot varchar(15));
insert into junk values('506N','506N418G06A-40');
select * from junk where lot like component+'%';
SELECT DISTINCT [LOT]
FROM WIPTEST
WHERE
[LOT] LIKE (ltrim(rtrim([Component])))+'%'
select DISTINCT [INVHDRBATCH], --[INVHDRDOC], [LOTSEQNBR],
[FGBATCH], [FG], [FGLABOR], [FGOH], [FGMAT], [FGCOST], [FGWEIGHT], [LABORLB], [OHLB], [MATLB], --[FORMULA], [FORMULAMAT], [FORMULALABOR], [FORMULAOH],
[Component], [LOT],
[LOTQTY], [LOTMATUNITCOST], [LOTMATWIPCOST], [LOTLABORWIPCOST],
[LOTOHWIPCOST]
from WIPTEST
INNER JOIN IV30200 on WIPTEST.[INVHDRBATCH] = IV30200.BACHNUMB AND WIPTEST.[INVHDRDOC] = IV30200.DOCNUMBR
LEFT OUTER JOIN FMMAST ON WIPTEST.[Component] = FMMAST.[FormulaId]
where [FG] = '506' and [FGBATCH] = '50418G12D' and [INVHDRBATCH] = '50418G12D'
select charindex('abc ','abcdef') -- = 0 ie a zero result means it doesnt exist
select charindex(rtrim('abc '),'abcdef') -- = 1 ie test for = 1 if at start of string 'abcdef'
select charindex(rtrim('def '),'abcdef') -- = 4 ie test for > 1 if anywhere in string 'abcdef'
select top (10) lot,component from wiptest
LOT's data type in that table is char(21). Perhaps, that's what the cause is. How do I overcome it?
Create table #wiptest_char(id int identity, lot char(21), component char(30))
insert #wiptest_char(lot,component) values ('LOT1 ','LOT1A ')
insert #wiptest_char(lot,component) values ('LOT2 ','LOT 2A ')
select lot,component,charindex(rtrim(lot),component),charindex(lot,component)
from #wiptest_char
where component like rtrim(lot)+'%'
Cannot recall if LOT is the short one (ie inside component) or COMPONENT is the short one (ie inside lot)insert #wiptest_char(lot,component) values ('LOT1A ','LOT1 ')
insert #wiptest_char(lot,component) values ('LOT2A ','LOT 2 ')
select lot,component,charindex(rtrim(component),lot),charindex(component,lot)
from #wiptest_char
where lot like rtrim(component)+'%'
select 'Found ShortStr in LongStr',ShortStr,LongStr
from (values ('LOT1','LOT1A'),('LOT1','LOT 1A'),('LOT1','LOT1 '),('LOT1'+char(10),'LOT1B')) WIP(ShortStr,LongStr)
where LongStr like ShortStr+'%'
If you run the above without the where clause, you would wonder why LOT1B doesnt show....
If not you may try ILIKE instead of LIKE to avoid case sensitivity issues