sql 2008R2不使用like的模糊查询,该如何处理
sql 2008R2不使用like的模糊查询
问题如下:
现在有一个表字段是这样的
字段1 字段2
73 83
73-09 83-90
73-10 83-20
74 52
74-573 52-830
想要得到的结果是
字段1 字段2
73 83
73-09 83-90
73-10 83-20
如果用liek的话应该是这样 select * from table 字段1 leke %73% and 字段2 like 83 !
但是现在我不想用like语句查询,请问有什么办法呢?
------解决方案--------------------
这样?
------解决方案--------------------
------解决方案--------------------
方法很多,要看你具体的需求
问题如下:
现在有一个表字段是这样的
字段1 字段2
73 83
73-09 83-90
73-10 83-20
74 52
74-573 52-830
想要得到的结果是
字段1 字段2
73 83
73-09 83-90
73-10 83-20
如果用liek的话应该是这样 select * from table 字段1 leke %73% and 字段2 like 83 !
但是现在我不想用like语句查询,请问有什么办法呢?
------解决方案--------------------
这样?
----------------------------------------------------------------
-- Author :DBA_HuangZJ(發糞塗牆)
-- Date :2014-08-13 16:50:08
-- 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)
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([字段1] varchar(6),[字段2] varchar(6))
insert [huang]
select '73','83' union all
select '73-09','83-90' union all
select '73-10','83-20' union ALL
select '74','52' union all
select '74-573','52-830'
--------------开始查询--------------------------
select * from [huang]
WHERE CHARINDEX(','+'73',','+字段1)>0 AND CHARINDEX(','+'83',','+字段2)>0
----------------结果----------------------------
/*
字段1 字段2
------ ------
73 83
73-09 83-90
73-10 83-20
*/
------解决方案--------------------
select * from tableName where left(字段1,2)='73' and left(字段2,2)='83'
------解决方案--------------------
方法很多,要看你具体的需求
select * from tb
where SUBSTRING (字段1,0,3)=73 and SUBSTRING (字段2,0,3)=83