导航
  • 主页
  • 基础类
  • 应用实例
  • 新技术前沿

向高手求一句SQL语句

ljjable 2008-01-02 11:12:32
数据库结构入下
id newsdate name
1 2005-7-3 aa
2 2006-7-3 bb
3 2008-7-3 cc
想实现,比如在7月3号这天,显示出我数据库里newsdate这个字段里的所有7月3号的信息,不管是哪天的,请问怎么实现啊
...全文
48 点赞 收藏 7
写回复
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
areswang 2008-01-02
存储过程
create procedure test @date as datetime
as
select * from tb
where newsdate = @date

exec test '2005-7-3'
回复
dawugui 2008-01-02
create table tb(id int, newsdate datetime, name varchar(10))
insert into tb values(1, '2005-7-3', 'aa')
insert into tb values(2, '2006-7-3', 'bb')
insert into tb values(3, '2008-7-3', 'cc')
go

--方法一
select * from tb where month(newsdate) = 7 and day(newsdate) = 3
/*
id newsdate name
----------- ----------------------- ----------
1 2005-07-03 00:00:00.000 aa
2 2006-07-03 00:00:00.000 bb
3 2008-07-03 00:00:00.000 cc

(3 行受影响)
*/

--方法二
select * from tb where datepart(mm,newsdate) = 7 and datepart(day,newsdate) = 3
/*
id newsdate name
----------- ----------------------- ----------
1 2005-07-03 00:00:00.000 aa
2 2006-07-03 00:00:00.000 bb
3 2008-07-03 00:00:00.000 cc

(3 行受影响)
*/
drop table tb
回复

select *
from tablename
where month(newsdate) = month(getdate()) and datepart(day,newsdate) = datepart(day,getdate())

回复
jackyerror 2008-01-02
select *
from table
where extract (month from newsdate) = 7 and extract (day from newsdate) = 3;

回复
dawugui 2008-01-02
数据库结构入下
id newsdate name
1 2005-7-3 aa
2 2006-7-3 bb
3 2008-7-3 cc
想实现,比如在7月3号这天,显示出我数据库里newsdate这个字段里的所有7月3号的信息,不管是哪天的,请问怎么实现啊

select * from tb where month(newsdate) = 7 and day(newsdate) = 3
回复
-狙击手- 2008-01-02
select *
from tablename
where dapart(mm,newsdate) = 7 and datepart(d,newsdate) = 3
回复
wzjpsq 2008-01-02
用 datapart
回复
发动态
发帖子
MS-SQL Server
创建于2007-09-28

3.2w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
社区公告
暂无公告