Link to home
Start Free TrialLog in
Avatar of Naitik Gamit
Naitik GamitFlag for India

asked on

Convert Nverchar Value Contain Comma To Int In SQL

I have Nvarchar Value As '7055,7085,7090,7035,7040,7050,7053,7055'

I Want To Convert It To Int.

Using Cast Or Convert It throw Error As Conversion failed when converting the nvarchar value '7010,7035,7045,7050,7053,7055,7085,7090,7040' to data type int.

Help Me To Convert It To Int

Thank You.
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

What do you pretend to do? Split each number in the varchar field and convert it one by one to int?
Avatar of Naitik Gamit

ASKER

I want to use this converted int value to IN query as

Select A,B,C Fom Etc_Tbl Where ID IN ( )

When I am using nvarchar value it throw error as :
Conversion failed when converting the nvarchar value '7010,7035,7045,7050,7053,7055,7085,7090,7040' to data type int.

I am also try cast and convert but no change......
Etc_Tbl is INT data type?
And what is the column name for you varchar values?
yes............

Select A,B,C Fom Etc_Tbl Where ID IN ( )  

In my query ID is int and nvarchar field is named Role_CSV

so,i am trying as

Select A,B,C Fom Etc_Tbl Where ID IN ( Role_CSV )
Select A,B,C Fom Etc_Tbl Where ID IN (CAST(Role_CSV  AS INT ) )
Select A,B,C Fom Etc_Tbl Where ID IN (CONVERT(INT,Role_CSV ) )  
But it not convert Role_CSV to INT.
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try to think in another solution as for example why not to convert the INT column to VARCHAR and compare it in the WHERE clause:
Select A,B,C 
From Etc_Tbl 
WHERE Role_CSV LIKE '%'+CAST(ID AS VARCHAR)+'%'

Open in new window

Avatar of Thomas H.
Thomas H.

select a, b, c from etc_tbl
where charindex(cast(id as varchar(20)),'7040,7055,7090,7035,7040,7050,7053,7055') > 0