-2

下面是我的查询,我在其中尝试组合多个列值

SELECT DISTINCT  CONVERT(varchar, Em.ADDR1) - CONVERT(varchar, Em.ADDR2) - CONVERT(varchar, Em.ADDR3) - CONVERT(varchar, Dm.DIST_NAME) - CONVERT(varchar,Sm.STATE_NAME)
- CONVERT(varchar, Cm.COUNTRY_NAME) AS Expr1
FROM            EMP_MST AS Em INNER JOIN
  DESIG_MST AS Dsgm ON Em.DESIG_NO = Dsgm.DESIG_NO AND Dsgm.COMPANY_NO = Em.COMPANY_NO INNER JOIN
  DEPT_MST AS Dptm ON Em.DEPT_NO = Dptm.DEPT_NO AND Dptm.COMPANY_NO = Em.COMPANY_NO INNER JOIN
  COUNTRY_MST AS Cm ON Em.COUNTRY_NO = Cm.COUNTRY_NO INNER JOIN
  STATE_MST AS Sm ON Em.STATE_NO = Sm.STATE_NO AND Em.COUNTRY_NO = Sm.COUNTRY_NO INNER JOIN
  DIST_MST AS Dm ON Em.DIST_NO = Dm.DIST_NO AND Em.STATE_NO = Dm.STATE_NO AND Em.COUNTRY_NO = Dm.COUNTRY_NO LEFT OUTER JOIN
  EMP_MST AS MEm ON Em.MANAGER = MEm.EMP_NO
WHERE        (1 = 1) AND (Em.EMP_NO LIKE '%%') AND (Em.COMPANY_NO = 1)

当我执行 Query 时,我得到

错误:

操作数数据类型 varchar 对 sqlServer 中的减法运算符无效

4

1 回答 1

1

正如人们所说,要遵循确切的查询有点困难,但具体问题,如错误所述,是您在列上使用-运算符。varchar

如果您尝试连接varchar值,则应该使用+运算符,例如:

SELECT DISTINCT  CONVERT(varchar, Em.ADDR1) + CONVERT(varchar, Em.ADDR2) + CONVERT(varchar, Em.ADDR3) + CONVERT(varchar, Dm.DIST_NAME) + CONVERT(varchar,Sm.STATE_NAME)
+ CONVERT(varchar, Cm.COUNTRY_NAME) AS Expr1

如果这些列都varchar已经存在,那么您可能也不需要这些CONVERT功能。

于 2013-01-08T13:20:29.760 回答