sql语句按日期查询,急

woaifeixiong 2011-12-06 03:59:15
我有一张表存放历史记录,表名为History。日期字段为STtime,数据格式为datetime。比如2011-12-1 15:36:25 我写了一个存储过程。要从外面传值进来。这个值也是日期.但是传进来的值只有年月日。比如2011-12-1。没有时分秒.语句我怎么写就能查询出2011-12-1这一天所有的记录,并且把他显示出来.
...全文
1913 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
koumingjie 2011-12-06
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 t3121522520 的回复:]
引用 2 楼 geniuswjt 的回复:
SQL code

--这样也可以,可以用到索引,好点
declare @dt datetime='2011-11-1'
select * from History where STime>=@dt and STime<dateadd(day,1,@dt)


这个解释一下 - -!初级不理解 给位大牛帮忙解释谢谢。
[/Quote]

where 的=号左边如果是函数或者表达式的话,如果用到索引
koumingjie 2011-12-06
  • 打赏
  • 举报
回复
select * from History where STime>='2011-12-1 00:00:00.000' and STime<='2011-12-1 23:59:59.997'
一十七 2011-12-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 geniuswjt 的回复:]
SQL code

--这样也可以,可以用到索引,好点
declare @dt datetime='2011-11-1'
select * from History where STime>=@dt and STime<dateadd(day,1,@dt)
[/Quote]

这个解释一下 - -!初级不理解 给位大牛帮忙解释谢谢。
woaifeixiong 2011-12-06
  • 打赏
  • 举报
回复
ALTER proc [dbo].[proc_getZhanDianShuJuData]
@StationID varchar(50),
@stime varchar(200),--时间从
@etime varchar(200)--至
as
declare @dian1 decimal(18,2),@dian2 decimal(18,2),@liuliang1 decimal(18,2),@liuliang2 decimal(18,2)
set @dian1=(select sum(UsePower) from History where CONVERT(varchar(10),Testtime,120 )=@stime)
set @dian2=(select sum(UsePower) from History where CONVERT(varchar(10), Testtime, 120 )=@etime)
set @liuliang1=(select sum(Alltotalrunning) from History where CONVERT(varchar(10), Testtime, 120 )=@stime)
set @liuliang2=(select sum(Alltotalrunning) from History where CONVERT(varchar(10), Testtime, 120 )=@etime)
select distinct Stationinfo.StationName as 站名,@dian1 as 起始电量,@dian2 as 终止电量,@liuliang1 as 起始累计流量,@liuliang2 as 终止累计流量,
@dian2-@dian1 as 用电量,@liuliang2-@liuliang1 as 用水量,(@dian2-@dian1)/(@liuliang2-@liuliang1) as 吨水耗电 from History,Stationinfo
where History.StationName=@StationID and Testtime between @stime and @etime and History.StationName=Stationinfo.Agreement



这是我写的存储过程,但是为什么查询不出数据来?@stime是传进来的开始日期,@etime是结束日期
pengxuan 2011-12-06
  • 打赏
  • 举报
回复

if object_id('tb','U') is not null
drop table tb
go
create table tb
(
id int identity(1,1),
name datetime
)
go
insert into tb (name)
select '2011-10-10 12:13:14' union all
select '2011-12-01 10:10:10' union all
select '2011-12-01 08:10:10' union all
select '2011-10-09 23:11:13'
go
select *,[转换日期]=convert(varchar(10),name,120) from tb where convert(varchar(10),name,120)= '2011-12-01'
go
--用convert把日期时间类型的yyyy-mm-dd hh:mm:ss 转换成yyyy-mm-dd的就可以比较了
快溜 2011-12-06
  • 打赏
  • 举报
回复
select * from History where datediff(dd,STime,'2011-12-01')=0
xiaoyangyang0001 2011-12-06
  • 打赏
  • 举报
回复
select * from History where datediff(dd,STime,'2011-12-01')=0
美到心痛 2011-12-06
  • 打赏
  • 举报
回复
日期型可以直接和日期格式的字符串比較的~
--小F-- 2011-12-06
  • 打赏
  • 举报
回复
select * from History where datediff(dd,STime,'2011-12-01')=0


还写一种吧
--小F-- 2011-12-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 geniuswjt 的回复:]
SQL code


--这样也可以,可以用到索引,好点
declare @dt datetime='2011-11-1'
select * from History where STime>=@dt and STime<dateadd(day,1,@dt)
[/Quote]
恩 小伙子有前途。
geniuswjt 2011-12-06
  • 打赏
  • 举报
回复

--这样也可以,可以用到索引,好点
declare @dt datetime='2011-11-1'
select * from History where STime>=@dt and STime<dateadd(day,1,@dt)
geniuswjt 2011-12-06
  • 打赏
  • 举报
回复

select * from History where convert(varchar(10),STime,120)='2011-12-01'

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧