sql server - Sql regex query to match exact matches of the word -
my query
declare @values table ( value nvarchar(255) ) insert @values values('bwe'); insert @values values('bwt'); insert @values values('imo i'); insert @values values('imo ii'); insert @values values('bwe-e'); insert @values values('bwe-ep'); insert @values values('imo iii'); insert @values values('bwm-ep(s)'); insert @values values('bwm-ep'); select * @values select a.value @values inner join (select 'bwm-ep(s)' class_notation) b on ltrim(rtrim(b.class_notation)) '%[^a-z0-9]' + ltrim(rtrim(a.value)) + '[^a-z0-9]%' or ltrim(rtrim(b.class_notation)) ltrim(rtrim(a.value)) + '[^a-z0-9]%' or ltrim(rtrim(b.class_notation)) '%[^a-z0-9]' + ltrim(rtrim(a.value)) or ltrim(rtrim(b.class_notation)) = ltrim(rtrim(a.value))
it works in of cases of class notation . in particular case when have 'bwm-ep(s)' class notation gives both bwm-ep(s) , bwm-ep . result should bwm-ep(s) . wrong ?
as see answers though root cause problem second or condition . cannot exclude condition need when class notation bwe bwt . how can overcome ?
it's the
or ltrim(rtrim(b.class_notation)) ltrim(rtrim(a.value)) + '[^a-z0-9]%'
condition matches bwm-ep
value.
this makes sense; expression can reduced 'bmw-ep(s)' 'bmw-ep'[^a-z0-9]%
bmw-ep
record, true.
here's how works without that: http://www.sqlfiddle.com/#!3/e5251/6
Comments
Post a Comment