Click here to Skip to main content
15,911,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
i am having column in sql table which contain value as 002,00DL,005S(varchar data Type)
i want the output as ''002','00DL','005S'' to check IN Clause

ex select * from Employee where ID in ()
thanks in advance
Posted
Comments
John C Rayan 11-Mar-15 7:16am    
Do you want all them as a single string?
Sachin MCA2012 11-Mar-15 11:58am    
yes

You can use below script

SQL
DECLARE @Temp NVARCHAR(100)

SELECT @Temp = COALESCE(@Temp +',','')+ [ColumnName]
FROM Table
 
Share this answer
 
Don't add any Single Quotation To your String.
Just run this Split Function

SQL
CREATE FUNCTION [dbo].[Split]
(
	@List nvarchar(2000),
	@SplitOn nvarchar(5)
)  
RETURNS @RtnValue table 
(
		
	Id int identity(1,1),
	Value nvarchar(100)
) 
AS  
BEGIN
	While (Charindex(@SplitOn,@List)>0)
	Begin 
		Insert Into @RtnValue (value)
		Select Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1))) 
			Set @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List))
	End 
	Insert Into @RtnValue (Value)
	Select Value = ltrim(rtrim(@List))
	Return
END


and your Query
SQL
declare @name nvarchar(max);// Declaration
select @name=column from table Assign values to Variable
select * from Employee where ID in (select Value from dbo.Split(@name,','))
 
Share this answer
 
v2
SQL
declare @output varchar(max)
set @output = ''''
select @output = @output + '''' + cast(col as varchar) +  ''','  from table where col is not null
select LEFT(@output,LEN(@output)-1) + ''''
 
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