如何在LIKE语句中连接%和列名称

时间:2012-07-03 19:59:29

标签: sql sql-server sql-server-2008 sql-like

我正在尝试这个:

SELECT FacilityID, FacilityName, CMSProviderID, [Provider Number] 
FROM G2_Facility, SCIPHospitalCompare
WHERE [Provider Number] LIKE '%' + CMSProviderID + '%';

我得到了:

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value '%' to data type int.

我做错了什么?我看到很多例子做这样的事情,但由于某种原因它根本不起作用。

谢谢!

1 个答案:

答案 0 :(得分:8)

我认为[Provider Number]CMSProviderID是数字字段?

将数字字段转换为字符串以比较两者:

SELECT FacilityID, FacilityName, CMSProviderID, [Provider Number]  
FROM G2_Facility, SCIPHospitalCompare 
WHERE CAST([Provider Number] as nvarchar(50)) LIKE 
    '%' + CAST(CMSProviderID as nvarchar(50)) + '%';