如何用一个SQL语句找出离某日最近的一个日期?

bqj 2006-08-17 08:57:11
表中有一个日期字段LRRQ,如何根据需要,用一个SQL语句,找出距离指定日期最近的上一个录入日期?
...全文
372 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
九斤半 2006-08-17
  • 打赏
  • 举报
回复
create table test(LRRQ datetime)
insert test(LRRQ)
select '2006-8-16' union all
select '2006-8-17' union all
select '2006-8-11' union all
select '2006-8-18' union all
select '2006-8-13' union all
select '2006-8-12'
select * from test
go


create proc sp_dt
@dt datetime
as
select top 1 LRRQ from test
where datediff(d,LRRQ,@dt) >=0
order by datediff(d,LRRQ,@dt)
go

exec sp_dt '2006-8-15'
go

drop proc sp_dt
drop table test
go
mugua604 2006-08-17
  • 打赏
  • 举报
回复
真快!
九斤半 2006-08-17
  • 打赏
  • 举报
回复
order by datediff(d,LRRQ,指定日期)

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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