34,594
社区成员
发帖
与我相关
我的任务
分享
---测试数据---
if object_id('[t1]') is not null drop table [t1]
go
create table [t1]([AA] datetime,[BBB] varchar(3),[CCC] varchar(3))
insert [t1]
select '2010-8-1 10:11:11','CCC','DDD' union all
select '2010-8-3 10:11:11','GGG','TTT' union all
select '2010-8-5 10:11:11','WWW','QQQ' union all
select '2010-8-9 10:11:11','BBB','RRR'
---查询---
select * from(select top 1 * from t1 where datediff(dd,'2010-8-3',aa)>0 order by aa) a
union all
select * from(select top 1 * from t1 where datediff(dd,'2010-8-3',aa)=0 order by aa) a
union all
select * from(select top 1 * from t1 where datediff(dd,'2010-8-3',aa)<0 order by aa desc) b
---结果---
AA BBB CCC
----------------------- ---- ----
2010-08-05 10:11:11.000 WWW QQQ
2010-08-03 10:11:11.000 GGG TTT
2010-08-01 10:11:11.000 CCC DDD
(3 行受影响)
select max(AA) from 表t1 where AA<'2010-8-3'
UNION ALL
select min(AA) from 表t1 where AA>'2010-8-3'
---测试数据---
if object_id('[t1]') is not null drop table [t1]
go
create table [t1]([AA] datetime,[BBB] varchar(3),[CCC] varchar(3))
insert [t1]
select '2010-8-1 10:11:11','CCC','DDD' union all
select '2010-8-3 10:11:11','GGG','TTT' union all
select '2010-8-5 10:11:11','WWW','QQQ' union all
select '2010-8-9 10:11:11','BBB','RRR'
---查询---
select convert(varchar(10),aa,120) as AA,BBB,CCC from(select top 1 * from t1 where datediff(dd,'2010-8-3',aa)>0 order by aa) a
union all
select convert(varchar(10),aa,120) as AA,BBB,CCC from(select top 1 * from t1 where datediff(dd,'2010-8-3',aa)<0 order by aa desc) b
---结果---
AA BBB CCC
---------- ---- ----
2010-08-05 WWW QQQ
2010-08-01 CCC DDD
(2 行受影响)