+ -
当前位置:首页 → 问答吧 → sql语句按日期查询,急

sql语句按日期查询,急

时间:2011-12-06

来源:互联网

我有一张表存放历史记录,表名为History。日期字段为STtime,数据格式为datetime。比如2011-12-1 15:36:25 我写了一个存储过程。要从外面传值进来。这个值也是日期.但是传进来的值只有年月日。比如2011-12-1。没有时分秒.语句我怎么写就能查询出2011-12-1这一天所有的记录,并且把他显示出来.

作者: woaifeixiong   发布时间: 2011-12-06

SQL code

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

作者: geniuswjt   发布时间: 2011-12-06

SQL code

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

作者: geniuswjt   发布时间: 2011-12-06

引用 2 楼 geniuswjt 的回复:
SQL code


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

恩 小伙子有前途。

作者: fredrickhu   发布时间: 2011-12-06

SQL code
select * from History where datediff(dd,STime,'2011-12-01')=0


还写一种吧

作者: fredrickhu   发布时间: 2011-12-06

日期型可以直接和日期格式的字符串比較的~

作者: sjcss   发布时间: 2011-12-06

select * from History where datediff(dd,STime,'2011-12-01')=0

作者: xiaoyangyang0001   发布时间: 2011-12-06

SQL code
select * from History where datediff(dd,STime,'2011-12-01')=0

作者: ssp2009   发布时间: 2011-12-06

SQL code

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的就可以比较了

作者: pengxuan   发布时间: 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是结束日期

作者: woaifeixiong   发布时间: 2011-12-06