当前位置: 代码迷 >> Sql Server >> 简单 sql 语句求解答。大能在哪里。解决方法
  详细解决方案

简单 sql 语句求解答。大能在哪里。解决方法

热度:410   发布时间:2016-04-27 13:35:37.0
简单 sql 语句求解答。。大能在哪里。。。
select * from dbo.T_Money as tm  
where
(
CONVERT(varchar(10), tm.[datetime],120)>=CONVERT(varchar(10),[email protected]',120) or
CONVERT(varchar(10), tm.[datetime],120)<=CONVERT(varchar(10),[email protected]',120) 
)
  AND (tm.creater='' or tm.creater like '%'+ '' +'%') 


当or 为and 的时候:

[email protected] 为'',空的时候
[email protected] 为'' , 空的时候。
(查询不出来数据,我知道是条件不满足查询不出来)

当为or 的时候输入条件就没用。感觉数据还是全部查出来了。。

但是,我想要的是 [email protected] 和 @endtime 为空的时候就查询全部的数据,
当有一个时间的条件时,数据就按照那一个时间来查询。 当然有两个时间就按照开始结束时间来查询。


求解答。。。。求高手。。。



------解决方案--------------------
你可以给变量赋个默认值,

这样就不用在这里纠结了
------解决方案--------------------
或者语句前面判断一下 如果为'' 则 set @starttime='1900-01-01'
set @endtime='9999-12-31'
------解决方案--------------------
[email protected]@endtime是字符串

select * from dbo.T_Money as tm where
(@starttime is not null and @starttime <> '' and datediff(dd,@starttime,tm.[datetime]) > 0) or
(@endtime is not null and @endtime <> '' and datediff(dd,tm.[datetime],@endtime) >= 0) or
((@starttime is null or @starttime = '') and (@endtime is null or @endtime = '') and (tm.creater = '' or tm.creater is null))

[email protected] 和 @endtime 为空,你是查询全部的数据,貌似应该为如下:
select * from dbo.T_Money as tm where
(@starttime is not null and @starttime <> '' and datediff(dd,@starttime,tm.[datetime]) > 0) or
(@endtime is not null and @endtime <> '' and datediff(dd,tm.[datetime],@endtime) >= 0) or
((@starttime is null or @starttime = '') and (@endtime is null or @endtime = '') and (1 = 1))
  相关解决方案