日期:2014-05-17 浏览次数:20532 次
select id,'李四' as name from table where charindex(',李四,',','+Name+',')>0
USE test
GO
-->生成表table1
if object_id('table1') is not null
drop table table1
Go
Create table table1([id] smallint,[Name] nvarchar(9))
Insert into table1
Select 1,N'张三,李四,王五,'
Union all Select 2,N'李四,'
Union all Select 3,N'李四,张三,'
DECLARE @Name NVARCHAR(50),@sql NVARCHAR(MAX)
SET @Name=N'李四'
SELECT @sql=ISNULL(@sql+' Union Select ','Select ')+LTRIM(id)+' As id,N'''+REPLACE(Name,',',''' As Name Union all Select '+LTRIM(id)+' As id,N''')+''' As Name' FROM table1
EXEC ('Select * from ('+@sql+') As t Where t.Name='''+@Name+'''')
/*
id Name
----------- ----
1 李四
2 李四
3 李四
*/
SELECT id,@Name AS Name FROM table1 WHERE CHARINDEX(@Name,Name)>0
create table tyz(id int ,tname varchar(20))
insert tyz select 1, N'张三,李四,王五'
insert tyz select 2, N'李四'
insert tyz select 3, N'李四,张三'
insert tyz select 4, N'张三'
declare @name varchar(10)
set @name='李四'
select id,@name as name from tyz where substring(tname,charindex(@name,tname),len(@name))=@name