SQL 将整数列转换为 varchar 并与字符串进行比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14457947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cast integer column to varchar and compare with string
提问by Rajaram Shelar
How do I compare list of integers with integer columns in sql server. For example I have a column statusid
as int
. I want to get the result where statusid
in 1,4,8,9
. I kept all these integers in string and operate with the column statusid as below but i am facing error there. This string may be hard coded or parameter.
如何将整数列表与 sql server 中的整数列进行比较。例如,我有一列statusid
作为int
. 我想statusid
在1,4,8,9
. 我将所有这些整数保存在字符串中,并使用下面的列 statusid 进行操作,但我在那里遇到了错误。此字符串可能是硬编码或参数。
where Cast(statusid as varchar) in ('1,4,8,9');
Please suggest the solution.
请提出解决方案。
回答by John Woo
since the number are converted to string, the values on IN
clause should each be wrap with single quotes
由于数字已转换为字符串,因此IN
子句中的值应分别用单引号括起来
where Cast(statusid as varchar(20)) in ('1','4','8','9');
回答by brykneval
WHERE statusid
IN (1, 4, 8, 9);