Reputation: 11721
My query is
DECLARE @Values TABLE
(
value NVARCHAR(255)
)
INSERT INTO @Values VALUES('BWE');
INSERT INTO @Values VALUES('BWT');
INSERT INTO @Values VALUES('IMO I');
INSERT INTO @Values VALUES('IMO II');
INSERT INTO @Values VALUES('BWE-E');
INSERT INTO @Values VALUES('BWE-EP');
INSERT INTO @Values VALUES('IMO III');
INSERT INTO @Values VALUES('BWM-EP(s)');
INSERT INTO @Values VALUES('BWM-EP');
select * from @Values
SELECT a.value
FROM @Values a
INNER JOIN (SELECT 'BWM-EP(s)' AS CLASS_NOTATION) b
ON LTRIM(RTRIM(b.CLASS_NOTATION)) like '%[^a-z0-9]' + LTRIM(RTRIM(a.VALUE)) + '[^a-z0-9]%'
or LTRIM(RTRIM(b.CLASS_NOTATION)) like LTRIM(RTRIM(a.VALUE)) + '[^a-z0-9]%'
or LTRIM(RTRIM(b.CLASS_NOTATION)) like '%[^a-z0-9]' + LTRIM(RTRIM(a.VALUE))
or LTRIM(RTRIM(b.CLASS_NOTATION)) = LTRIM(RTRIM(a.VALUE))
It works in most of the cases of class notation . but in this particular case when I have 'BWM-EP(s)' as class notation it gives you both BWM-EP(s) and BWM-EP . But result should be only BWM-EP(s) . What is wrong ?
As I see from the answers even though the root cause for this problem is second OR condition . I cannot exclude that condition as i need that when the class notation is BWE BWT . How can i overcome from this ?
Upvotes: 2
Views: 960
Reputation: 121922
Try this one -
DECLARE @Values TABLE (value NVARCHAR(255))
INSERT INTO @Values
VALUES
('BWE'),('BWT'),('IMO I'),('IMO II'),
('BWE-E'),('BWE-EP'),('IMO III'),
('BWM-EP(s)'),('BWM-EP')
SELECT value
FROM (
SELECT value = LTRIM(RTRIM(value))
FROM @Values
) a
CROSS JOIN (
SELECT note = LTRIM(RTRIM('BWM-EP(s)'))
) t
where note LIKE '%[^a-z0-9]' + value + '[^a-z0-9]%'
OR note LIKE '%[^a-z0-9]' + value
OR note = value
Output -
value
----------
BWM-EP(s)
Upvotes: 1
Reputation: 40536
It's the
or LTRIM(RTRIM(b.CLASS_NOTATION)) like LTRIM(RTRIM(a.VALUE)) + '[^a-z0-9]%'
condition that matches the BWM-EP
value.
This makes sense; this expression can be reduced to 'BMW-EP(s)' like 'BMW-EP'[^a-z0-9]%
for the BMW-EP
record, which is obviously true.
Here's how it works without that: http://www.sqlfiddle.com/#!3/e5251/6
Upvotes: 2