现在有一个表a,一个字段b为nvarchar类型,存储几个字符串的集合,使用,分隔
假设a中有两条数据
b的值
第一条:1,5,10,16
第二条:4,6,8,15,100
现在要查询b中含有10的那条,应该怎么写sql
试过: select * from a where '10' in b 不行
select * from a where '%10%' like b 会出现错误数据
求解sql语句
------解决方案--------------------
----------------------------------------------------------------
-- Author :DBA_HuangZJ(發糞塗牆)
-- Date :2014-08-19 11:05:26
-- Version:
-- Microsoft SQL Server 2012 - 11.0.5058.0 (X64)
-- May 14 2014 18:34:29
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([b] varchar(12))
insert [tb]
select '1,5,10,16' union all
select '4,6,8,15,100'
--------------开始查询--------------------------
select * from [tb]
WHERE CHARINDEX(','+'10'+',',','+b+',')>0
----------------结果----------------------------
/*
b
------------
1,5,10,16
*/