Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have data in column of table as 1,2,3 whose data type is varchar(MAx) in sql server 2008
i want to convert it into '1','2','3'
how it possible in SQL
i try but it gives me error as Error converting data type varchar to numeric.
Posted
Updated 4-May-12 20:38pm
v3
Comments
Sandeep Mewara 5-May-12 2:28am    
By writing a query!

Did you try anything?

Thats actually not simple - SQL does not have a concept of an array!
However, you can convert it into a temporary table, and use that. This is a stored procedure which accepts a list of commas separated values and uses that as the IN parameter of a query - pretty much what you are trying to do.
SQL
Pass a list as a string parameter:
DECLARE @INSTR as VARCHAR(MAX)
SET @INSTR = '2,3,177,'
DECLARE @SEPERATOR as VARCHAR(1)
DECLARE @SP INT
DECLARE @VALUE VARCHAR(1000)
SET @SEPERATOR = ','
CREATE TABLE #tempTab (id int not null)
WHILE PATINDEX('%' + @SEPERATOR + '%', @INSTR ) <> 0
BEGIN
   SELECT  @SP = PATINDEX('%' + @SEPERATOR + '%',@INSTR)
   SELECT  @VALUE = LEFT(@INSTR , @SP - 1)
   SELECT  @INSTR = STUFF(@INSTR, 1, @SP, '')
   INSERT INTO #tempTab (id) VALUES (@VALUE)
END
SELECT * FROM myTable WHERE id IN (SELECT id FROM #tempTab)
DROP TABLE #tempTab
 
Share this answer
 
Comments
Maciej Los 5-May-12 4:49am    
Great job! My 5 ;)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900