查询数据表SNUM里的NUM,CNUM,CDATE,CTIME,CL,L,F选项,表里存放的是24个传感器的所有数据
条件:起始日期放在date1里 (如2008-4-21) 起始时间在time1里(15:18:12) 终止日期data2里 终止时间在time2里 再就有传感器用一个数组表示strs[24],当用户选中第i个传感器时时strs[i]=1,可能有多个传感器被选中.
CDATE表里的日期 (2008/4/21形式) CTIME是时间 (15:18:12)
目前要实现的功能就是从表SNUM里查询符合用户选择的从起始日期起始时间开始到终止日期终止时间的某一个传感器或多个传感器的所有数据,怎么实现,在线等待!
条件:起始日期放在date1里 (如2008-4-21) 起始时间在time1里(15:18:12) 终止日期data2里 终止时间在time2里 再就有传感器用一个数组表示strs[24],当用户选中第i个传感器时时strs[i]=1,可能有多个传感器被选中.
CDATE表里的日期 (2008/4/21形式) CTIME是时间 (15:18:12)
目前要实现的功能就是从表SNUM里查询符合用户选择的从起始日期起始时间开始到终止日期终止时间的某一个传感器或多个传感器的所有数据,怎么实现,在线等待!
解决方案 »
- 数据库一列字段拆分搜索的问题......
- 关于存储过程里的变量应用的问题2
- 时间转字符串
- 请问我想在insert触发器里写一个SQL语句,能不能获取到底是哪个机器或客户端在向这个表插入数据呢
- 急求:Sql server2000的另一电脑中的实例注册问题?Sql server不存在访问被拒绝
- 临时表相关问提
- 关于求平均值的问题,高手请进
- 有关字段类型的问题,请指教。
- 有那位老兄给我介绍一下ORALCE 的SVRMGRL,这里面的东东我不太熟悉!
- 在SELECT 语句里怎么增加一列,并定义该列的长度呢??
- 触发器如何使用呢?也就是说在VB代码中的什么位置调用触发器呢,请举例
- 循环中变量赋值的问题~~!
如@s = '1,2,3,4.....'select *
from snum
where cast( cast(cdate as varchar)+' '+cast(ctime as varchar) as datetime)
between @dt1 and @dt2
and charindex(','+ltrim(cnum)+',',','+@s+',') > 0
/*
标题:分解字符串并查询相关数据
作者:爱新觉罗.毓华
时间:2008-03-18
地点:广东深圳
说明:通过使用函数等方法分解字符串查询相关数据。问题:通过分解一个带某种符号分隔的字符串在数据库中查找相关数据。
例如 @str = '1,2,3',查询下表得到记录1,4,5,6
ID TypeID
1 1,2,3,4,5,6,7,8,9,10,11,12
2 2,3
3 3,7,8,9
4 2,6
5 4,5
6 6,7
*/
-----------------------------
create table tb (ID int , TypeID varchar(30))
insert into tb values(1 , '1,2,3,4,5,6,7,8,9,10,11,12')
insert into tb values(2 , '2,3')
insert into tb values(3 , '3,7,8,9')
insert into tb values(4 , '2,6')
insert into tb values(5 , '4,5')
insert into tb values(6 , '6,7')
go
-----------------------------
--如果仅仅是一个,如@str = '1'.
declare @str as varchar(30)
set @str = '1'
select * from tb where charindex(',' + @str + ',' , ',' + TypeID + ',') > 0
select * from tb where ',' + TypeID + ',' like '%,' + @str + ',%'
/*
ID TypeID
----------- ------------------------------
1 1,2,3,4,5,6,7,8,9,10,11,12
(所影响的行数为 1 行)
*/-----------------------------
--如果包含两个,如@str = '1,2'.
declare @str as varchar(30)
set @str = '1,2'
select * from tb where charindex(',' + left(@str , charindex(',' , @str) - 1) + ',' , ',' + typeid + ',') > 0 or
charindex(',' + substring(@str , charindex(',' , @str) + 1 , len(@str)) + ',' , ',' + typeid + ',') > 0
select * from tb where ',' + typeid + ',' like '%,' + left(@str , charindex(',' , @str) - 1) + ',%' or
',' + typeid + ',' like '%,' + substring(@str , charindex(',' , @str) + 1 , len(@str)) + ',%'
/*
ID TypeID
----------- ------------------------------
1 1,2,3,4,5,6,7,8,9,10,11,12
2 2,3
4 2,6
(所影响的行数为 3 行)
*/-------------------------------------------
--如果包含三个或四个,用PARSENAME函数来处理.
declare @str as varchar(30)
set @str = '1,2,3,4'
select * from tb where
charindex(',' + parsename(replace(@str , ',' , '.') , 4) + ',' , ',' + typeid + ',') > 0 or
charindex(',' + parsename(replace(@str , ',' , '.') , 3) + ',' , ',' + typeid + ',') > 0 or
charindex(',' + parsename(replace(@str , ',' , '.') , 2) + ',' , ',' + typeid + ',') > 0 or
charindex(',' + parsename(replace(@str , ',' , '.') , 1) + ',' , ',' + typeid + ',') > 0
select * from tb where
',' + typeid + ',' like '%,' + parsename(replace(@str , ',' , '.') , 4) + ',%' or
',' + typeid + ',' like '%,' + parsename(replace(@str , ',' , '.') , 3) + ',%' or
',' + typeid + ',' like '%,' + parsename(replace(@str , ',' , '.') , 2) + ',%' or
',' + typeid + ',' like '%,' + parsename(replace(@str , ',' , '.') , 1) + ',%'
/*
ID TypeID
----------- ------------------------------
1 1,2,3,4,5,6,7,8,9,10,11,12
2 2,3
3 3,7,8,9
4 2,6
5 4,5
(所影响的行数为 5 行)
*/---------------------------------------
--如果超过四个,则只能使用函数或动态SQL来分解并查询数据。
/*
名称:fn_split函数.
功能:实现字符串分隔功能的函数
*/
create function dbo.fn_split(@inputstr varchar(8000), @seprator varchar(10))
returns @temp table (a varchar(200))
as
begin
declare @i int
set @inputstr = rtrim(ltrim(@inputstr))
set @i = charindex(@seprator , @inputstr)
while @i >= 1
begin
insert @temp values(left(@inputstr , @i - 1))
set @inputstr = substring(@inputstr , @i + 1 , len(@inputstr) - @i)
set @i = charindex(@seprator , @inputstr)
end
if @inputstr <> '\'
insert @temp values(@inputstr)
return
end
go--调用
declare @str as varchar(30)
set @str = '1,2,3,4,5'select distinct m.* from tb m,
(select * from dbo.fn_split(@str,',')) n
where charindex(',' + n.a + ',' , ',' + m.typeid + ',') > 0drop table tb
drop function dbo.fn_split /*
ID TypeID
----------- ------------------------------
1 1,2,3,4,5,6,7,8,9,10,11,12
2 2,3
3 3,7,8,9
4 2,6
5 4,5
(所影响的行数为 5 行)
*/------------------------------------------
--使用动态SQL的语句。
declare @str varchar(200)
declare @sql as varchar(1000)
set @str = '1,2,3,4,5'
set @sql = 'select ''' + replace(@str , ',' , ''' as id union all select ''')
set @sql = @sql + ''''
set @sql = 'select distinct a.* from tb a , (' + @sql + ') b where charindex(' + ''','' + b.id + ' + ''',''' + ' , ' + ''','' + a.typeid + ' + ''',''' + ') > 0 '
exec (@sql)
/*
ID TypeID
----------- ------------------------------
1 1,2,3,4,5,6,7,8,9,10,11,12
2 2,3
3 3,7,8,9
4 2,6
5 4,5
(所影响的行数为 5 行)
*/
如@s = '1,2,3,4.....' select *
from snum
where cast( cast(cdate as varchar)+' '+cast(ctime as varchar) as datetime)
between @dt1 and @dt2
and charindex(','+ltrim(cnum)+',',','+@s+',') > 0