用通配符号来做啊select * from table where field like 'ss%',ss这里是一个字符,你可以把他做成变量如果要用函数,substring可以截取子串.
select * from table where left(field,2)='ss'
create table table1 (字段 varchar(20)) insert table1 select 'ac...ddd' union all select 'b....ddd' union all select 'ac..ddd' union all select 'ac..ded'declare @chvStr varchar(10) declare @sql varchar(1024) set @chvStr='ac' set @sql='select * from table1 where 字段 like '''+@chvStr+'%''' select @sql exec (@sql)drop table table1 --结果 select * from table1 where 字段 like 'ac%'(所影响的行数为 1 行)字段 -------------------- ac...ddd ac..ddd ac..ded
left简单些:select * from table1 where left(字段,2)=@chvStr--第一种方式对表变量和临时表可能不合适。自己组合的sql句子一般都有这个问题。
insert table1 select 'ac...ddd'
union all select 'b....ddd'
union all select 'ac..ddd'
union all select 'ac..ded'declare @chvStr varchar(10)
declare @sql varchar(1024)
set @chvStr='ac'
set @sql='select * from table1 where 字段 like '''+@chvStr+'%'''
select @sql
exec (@sql)drop table table1
--结果
select * from table1 where 字段 like 'ac%'(所影响的行数为 1 行)字段
--------------------
ac...ddd
ac..ddd
ac..ded