Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast integer column to varchar and compare with string

Tags:

sql

sql-server

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.

where Cast(statusid as varchar) in ('1,4,8,9');

Please suggest the solution.

like image 519
Rajaram Shelar Avatar asked Jan 22 '13 11:01

Rajaram Shelar


People also ask

Can we convert int to varchar in SQL?

CAST Function to convert int to string In this example, we are converting the OrderQty which is an integer into varchar with SELECT CAST syntax.

Which is faster CAST or convert SQL Server?

In our demo, that is performed using three different data types conversions, it can be found that CAST is the fastest function that can be used to convert the data type of the provided value, and the PARSE function is the slowest.


1 Answers

since the number are converted to string, the values on IN clause should each be wrap with single quotes

where Cast(statusid as varchar(20)) in ('1','4','8','9');
like image 62
John Woo Avatar answered Oct 15 '22 20:10

John Woo