How to use column name as part of a LIKE statement in a WHERE clause of a JOIN

▼魔方 西西 提交于 2019-12-11 01:27:25

问题


LEFT OUTER JOIN [INVENTTRANS]
ON #TEMP.VOUCHERPHYSICAL=[INVENTTRANS].VOUCHERPHYSICAL
WHERE [INVENTTRANS].ITEMID = #Temp.INVENTDIMID

This provides me nearly the result I am looking for. About 1/4th of the desired results are eliminated because INVENTDIMID has a suffix appended to it in certain cases.

What is the proper syntax to succeed in a like clause similar to this intent.

WHERE '[INVENTTRANS].ITEMID%' like #Temp.INVENTDIMID

Alternatively, if there is no short hand way to do this, what is the most effective long handed way about it.

Thanks


回答1:


I am not sure if I completely understand the question but it appears you want to apply a predicate to a like statement that compares something else that is fixed and not in text you supply.

I always do something like this:

Where (ColumnName) like '%' + (OtherColumnName) + '%'

This is basically applying a wildcard around either side of the column you are trying to relate to. It will work for variables or columns.

UPDATE 5-21-13

Simple example of two table variables that self populate and work with wild card matcing on the second table variable. Code will run as is on SQL 2008 or higher in SQL Management Studio:

declare @Person Table ( personID int identity, person varchar(8));

insert into @Person values ('Brett'),('Sean'),('Chad'),('Michael'),('Ray'),('Erik'),('Queyn');

declare @Match table ( Description varchar(4));

insert into @Match values ('B'), ('S'),('e')

Select top 100
    p.*
,   m.Description as 'MatchedOn'
from @Person p, @Match m
where p.person like '%' + m.Description + '%'


来源:https://stackoverflow.com/questions/16594460/how-to-use-column-name-as-part-of-a-like-statement-in-a-where-clause-of-a-join

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!