例: table0表
id txt内容
----------------------------------
1 03|02|01|
2 05|03|06|
3 07|08|03|
4 09|02|03| txt内容 都是已 | 结尾的数序
我现在想选择 txt内容中包含 03| 的行 但不包含 02|既结果 id 是 2 3 怎样用简单的SQL实现 请教
id txt内容
----------------------------------
1 03|02|01|
2 05|03|06|
3 07|08|03|
4 09|02|03| txt内容 都是已 | 结尾的数序
我现在想选择 txt内容中包含 03| 的行 但不包含 02|既结果 id 是 2 3 怎样用简单的SQL实现 请教
where charindex('03|',txt内容)>0 and charindex('02|',txt内容)<0
-- Author : htl258(Tony)
-- Date : 2010-04-26 16:14:32
-- Version:Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
-- Blog : http://blog.csdn.net/htl258
--------------------------------------------------------------------------
--> 生成测试数据表:table0IF NOT OBJECT_ID('[table0]') IS NULL
DROP TABLE [table0]
GO
CREATE TABLE [table0]([id] INT,[txt内容] NVARCHAR(10))
INSERT [table0]
SELECT 1,N'03|02|01|' UNION ALL
SELECT 2,N'05|03|06|' UNION ALL
SELECT 3,N'07|08|03|' UNION ALL
SELECT 4,N'09|02|03|'
GO
--SELECT * FROM [table0]-->SQL查询如下:
select * from table0 where charindex('|03|','|'+txt内容+'|')>0 and charindex('|02|','|'+txt内容+'|')=0/*
id txt内容
----------- ----------
2 05|03|06|
3 07|08|03|(2 行受影响)
*/
---------------------------------------------------------------
-- DESIGNER :happycell188(喜喜)
-- QQ :584738179
-- Development Tool :Microsoft Visual C++ 6.0 C Language
-- FUNCTION :CONVERT DATA TO T-SQL
---------------------------------------------------------------
-- Microsoft SQL Server 2005
-- Developer Edition on Microsoft Windows XP [版本 5.1.2600]
---------------------------------------------------------------
---------------------------------------------------------------use test
go
if object_id('test.dbo.tb') is not null drop table tb
-- 创建数据表
create table tb
(
id int,
txt内容 char(10)
)
go
--插入测试数据
insert into tb select 1,'03|02|01|'
union all select 2,'05|03|06|'
union all select 3,'07|08|03|'
union all select 4,'09|02|03|'
go
--代码实现select id from tb a where txt内容 like '%03|%' and not exists
(select * from tb b where a.id=b.id and txt内容 like '%02|%')/*测试结果id
--------------
2
3(2 行受影响)
*/