一、查询某值是否在以逗号隔开字段内
charindex(','+@查詢字符+',',',+@某個字符串或字段+',')>0
SELECT COUNT(1) FROM [tb] where charindex(',2,',','+[
rolesGroup]+',')>0
二、查询方括号或者小口号中间包含某个字符的
例如:
形如:模糊[模糊01模糊]模糊
charindex('[',[Content])<charindex('01',[Content]) and charindex('01',[Content])<charindex(']',[Content]) and charindex('[',[Content])>0
三、查询判断字符串包含中文
PATINDEX('%[啊-座]%',[Name])>0 或
PATINDEX('%[吖-座]%',[Name])>0
四、随机查询N条数据
select top 10 * from tb order by newid()
五、获取自增ID缺失号
select id+1 from [KQ] t where not exists (select 1 from [KQ] where id=t.id+1) and t.id< (select max(id) from [KQ]) order by id