日期:2014-05-17  浏览次数:20465 次

数据库中使用正则表达式
数据库中有一个字段有Tb23456,TBS123456,tasd1234,DFS1243456这些类型,过滤掉只要Tb23456这个类型的呢?
------解决方案--------------------
如果你那些以逗号为间隔每行显示的话,直接where里面登于Tb23456就可以拉
------解决方案--------------------

select * from 表 where 字段='Tb23456'

------解决方案--------------------
引用:
select * from 表 where 字段 like 'Tb%'


select * from 表 where left(字段,2) = 'Tb'

------解决方案--------------------
引用:
Quote: 引用:


select * from [表名] 
 where patindex('tb%',[字段])>0

假如我还要过滤掉TBS开头的呢,有什么好方法吗?


试试这个:
select * from [表名] 
 where patindex('tb%',[字段])>0 and 
       patindex('tbs%',[字段]) =0
 

------解决方案--------------------
----------------------------------------------------------------
-- Author  :DBA_Huangzj(發糞塗牆)
-- Date    :2013-11-28 11:30:45
-- 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]([a] varchar(10))
insert [huang]
select 'Tb23456' union all
select 'TBS123456' union ALL
select 'tasd1234' union all
select 'DFS1243456'
--------------开始查询--------------------------

select * from [huang] WHERE a LIKE 'tb[0-9]%'
----------------结果----------------------------
/* 
a
----------
Tb23456
*/