为什么这个SQL转换为bigint不起作用?

时间:2015-10-19 03:25:38

标签: sql sql-server

我内部加入了几个SQL Server表,并且有一个varchar列需要与数字列匹配。我正在尝试将该列转换为bigint,但它会出现

  

将数据类型varchar转换为bigint时出错

试图这样做时。任何人都可以指出我正确的方向如何解决这个问题?谢谢!

SELECT 
    userid, COUNT(DISTINCT maintlist.maintid) as thecount 
FROM 
    Userlist 
INNER JOIN 
    maintlist ON CONVERT(bigint, MaintList.enteredby) = userid 
INNER JOIN
    maintnotes ON maintnotes.maintid = maintlist.maintid 
WHERE
    deptnum <> '0' 
    AND notedate BETWEEN '2015-10-01' AND '11/1/2015' 
GROUP BY 
    userid 
ORDER BY  
    thecount DESC; 

2 个答案:

答案 0 :(得分:1)

如果maintlist表中的某些用户ID无法从varchar转换为bigint,则将userlist表中的用户ID转换为varchar意味着您的代码将起作用,但您的联接将不会提供您想要的结果。所以我仍然会寻找无法转换的用户ID。

答案 1 :(得分:0)

感谢您的帮助,我能够将userid转换为varchar。

SELECT userid, COUNT(DISTINCT maintlist.maintid) as thecount from Userlist inner join maintlist ON MaintList.enteredby = convert(varchar(20),userid) inner join maintnotes on maintnotes.maintid = maintlist.maintid where deptnum <> '0' AND notedate between '2015-10-01' AND '11/1/2015' group by userid order by thecount DESC;