日期:2014-05-18  浏览次数:20453 次

查询某Varchar列是否包含某一字符串的Sql语句
table1中有一列col1,类型为varchar,其中有一行col1的内容为'abcdef'

要实现

select * from table1 where 'cde' in col1

或者

select * from table1 where col1 have 'cde'

该怎么写SQL语句呢?

------解决方案--------------------
SQL code
select * from table1 where col1 like '%cde%'

--或是

select * from table1 where charindex('cde',col1)>0

------解决方案--------------------
SQL code

declare @table1 table (col1 varchar(6))
insert into @table1
select 'aaa' union all
select 'abcdef' union all
select 'asdf' union all
select 'gasd' union all
select 'ssss'

select * from @table1 where col1 like '%cde%'
--或是
select * from @table1 where charindex('cde',col1)>0 

/*
col1
------
abcdef
*/

------解决方案--------------------
探讨
SQL code

declare @table1 table (col1 varchar(6))
insert into @table1
select 'aaa' union all
select 'abcdef' union all
select 'asdf' union all
select 'gasd' union all
select 'ssss'

select *……

------解决方案--------------------
探讨
SQL code
select * from table1 where col1 like '%cde%'

--或是

select * from table1 where charindex('cde',col1)>0

------解决方案--------------------
SQL code
select * from table1 where col1 like '%cde%'

------解决方案--------------------
探讨

SQL code
select * from table1 where col1 like '%cde%'

--或是

select * from table1 where charindex('cde',col1)>0