日期:2014-05-17 浏览次数:20539 次
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2014-01-24 12:46:51
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([ID] int,[postlevelIDs] varchar(5))
insert [huang]
select 1,'[3]' union all
select 2,'[3,5]' union all
select 3,'[2,6]' union all
select 4,'[]'
--------------开始查询--------------------------
select * from [huang]
WHERE CHARINDEX(','+'3'+',', ','+SUBSTRING([postlevelIDs],2,LEN([postlevelIDs])-2)+',')>0
----------------结果----------------------------
/*
ID postlevelIDs
----------- ------------
1 [3]
2 [3,5]
*/
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ID] int,[postlevelIDs] varchar(5))
insert [tb]
select 1,'[3]' union all
select 2,'[3,5]' union all
select 3,'[2,6]' union all
select 4,'[]'
go
select *
from tb
where CHARINDEX(','+'3'+',' , ','+replace(REPLACE([postlevelIDs],'[',''),']','')+',') > 0
/*
ID postlevelIDs
1 [3]
2 [3,5]
*/