SELECT * FROM TABLE WHERE DATEDIFF(day, BIRTH_DATE,GETDATE())>7 这个肯定不行的。
上面的都没有考虑到年这个问题。楼主试试这个,可以实现。Select * from [table] Where DateDiff(dd,Convert(DateTime,Convert(Varchar(6),birth_date,110)+'05'),GetDate())<7 And DateDiff(dd,Convert(DateTime,Convert(Varchar(6),birth_date,110)+'05'),GetDate())>=0我看还能不能简化点。
SELECT * FROM 表 WHERE DATEDIFF(day, 字段,GETDATE())>7可以的
SELECT * FROM TABLE WHERE DATEDIFF(day, birth_date,GETDATE())>7 這個行。 沒有問題 其中datediff()這個函數的運用了
--建立测试环境 Create table [table] (ID Int, birth_date DateTime) --插入数据 Insert [table] Values(1,'1981/12/26') Insert [table] Values(2,'1977/02/26') Insert [table] Values(3,'1978/01/05') Insert [table] Values(4,'1979/01/01') Insert [table] Values(5,'1980/12/27') Insert [table] Values(6,'1980/12/25') GO --测试 Declare @I Int Declare @Date DateTime Set @I=8 --天数 Set @Date='2005/01/02' --当前日期 Select * from [table] Where (DateDiff(dd,Convert(Varchar(6),birth_date,110)+'05',@Date)<=@I And DateDiff(dd,Convert(Varchar(6),birth_date,110)+'05',@Date)>=0) Or (DateDiff(dd,Convert(Varchar(6),birth_date,110)+'05',@Date)<-(365-@I)) --删除测试环境 Drop table [table] --结果 /* ID birth_date 1 1981-12-26 00:00:00.000 4 1979-01-01 00:00:00.000 5 1980-12-27 00:00:00.000 */
Create table [table] (ID Int, birth_date DateTime) --插入数据 Insert [table] Values(1,'1981/12/26') Insert [table] Values(2,'1977/02/26') Insert [table] Values(3,'1978/01/05') Insert [table] Values(4,'1979/01/01') Insert [table] Values(5,'1980/12/27') Insert [table] Values(6,'1980/12/25') GO declare @date datetime set @date='2005/1/2' select * from [table] where (case when convert(varchar(5),@date-7,1)>convert(varchar(5),@date,1) then (case when convert(varchar(5),birth_date,1)<convert(varchar(5),@date-7,1) and convert(varchar(5),birth_date,1)>convert(varchar(5),@date,1) then 0 else 1 end) else (case when convert(varchar(5),birth_date,1) between convert(varchar(5),@date-7,1) and convert(varchar(5),@date,1) then 1 else 0 end) end)=1drop table [table]
我的思路是:如果7天前的日期(月日)大于这一天的(月日),则 生日大于这一天且小于7天前的日期 都不满足,其它日期都满足 如果7天前的日期(月日)小于这一天的(月日),则用between ... and ...
再加上年份限制应该可以了. SELECT * FROM ls_TABLE5 WHERE DATEDIFF(day, birth_date,GETDATE())>7 and year(getdate())-year(birth_date)<=1
Select * from [table] Where DateDiff(dd,Convert(DateTime,Convert(Varchar(6),dateadd(dd,7,birth_date),110)+'05'),dateadd(dd,7,GetDate())<7 And DateDiff(dd,Convert(DateTime,Convert(Varchar(6),dateadd(dd,7,birth_date),110)+'05'),dateadd(dd,7,GetDate())>=0
SELECT * FROM TABLE WHERE DATEDIFF(day, BIRTH_DATE,GETDATE())>7
一条纪录:他的birth_date值为:1971-01-10 00:00:00.000
能和getdate()比较吗?
这个肯定不行的。
Where DateDiff(dd,Convert(DateTime,Convert(Varchar(6),birth_date,110)+'05'),GetDate())<7 And DateDiff(dd,Convert(DateTime,Convert(Varchar(6),birth_date,110)+'05'),GetDate())>=0我看还能不能简化点。
這個行。
沒有問題
其中datediff()這個函數的運用了
--建立测试环境
Create table [table]
(ID Int,
birth_date DateTime)
--插入数据
Insert [table] Values(1,'1981/12/26')
Insert [table] Values(2,'1977/02/26')
Insert [table] Values(3,'1978/01/05')
Insert [table] Values(4,'1979/01/01')
Insert [table] Values(5,'1980/12/27')
Insert [table] Values(6,'1980/12/25')
GO
--测试
Declare @I Int
Declare @Date DateTime
Set @I=8 --天数
Set @Date='2005/01/02' --当前日期
Select * from [table] Where
(DateDiff(dd,Convert(Varchar(6),birth_date,110)+'05',@Date)<=@I
And DateDiff(dd,Convert(Varchar(6),birth_date,110)+'05',@Date)>=0)
Or (DateDiff(dd,Convert(Varchar(6),birth_date,110)+'05',@Date)<-(365-@I))
--删除测试环境
Drop table [table]
--结果
/*
ID birth_date
1 1981-12-26 00:00:00.000
4 1979-01-01 00:00:00.000
5 1980-12-27 00:00:00.000
*/
SELECT * FROM TABLE WHERE DATEDIFF(day, birth_date,GETDATE())>7
這個行。
沒有問題
其中datediff()這個函數的運用了
----------------------------------------
这样不行的。就我上面的数据。查询的结果是
ID birth_date
1 1981-12-26 00:00:00.000
2 1977-02-26 00:00:00.000
3 1978-01-05 00:00:00.000
4 1979-01-01 00:00:00.000
5 1980-12-27 00:00:00.000
6 1980-12-25 00:00:00.000都说了这样写,没有考虑好年份的。
from t
where datediff(d,dateadd(y,datediff(y,getdate(),billdate),billdate),getdate())<7
回复人: Hopewell_Go(好的在后頭﹗希望更好﹗﹗) ( ) 信誉:100 2005-04-29 17:32:00 得分: 0
SELECT * FROM TABLE WHERE DATEDIFF(day, birth_date,GETDATE())>7
這個行。
沒有問題
其中datediff()這個函數的運用了
----------------------------------------
这样不行的。就我上面的数据。查询的结果是
ID birth_date
1 1981-12-26 00:00:00.000
2 1977-02-26 00:00:00.000
3 1978-01-05 00:00:00.000
4 1979-01-01 00:00:00.000
5 1980-12-27 00:00:00.000
6 1980-12-25 00:00:00.000都说了这样写,没有考虑好年份的。
----昏,這樣來計算得話,逐一-365天 ,並要考慮366天得
(ID Int,
birth_date DateTime)
--插入数据
Insert [table] Values(1,'1981/12/26')
Insert [table] Values(2,'1977/02/26')
Insert [table] Values(3,'1978/01/05')
Insert [table] Values(4,'1979/01/01')
Insert [table] Values(5,'1980/12/27')
Insert [table] Values(6,'1980/12/25')
GO
declare @date datetime
set @date='2005/1/2'
select * from [table] where
(case when convert(varchar(5),@date-7,1)>convert(varchar(5),@date,1) then
(case when convert(varchar(5),birth_date,1)<convert(varchar(5),@date-7,1) and convert(varchar(5),birth_date,1)>convert(varchar(5),@date,1) then 0 else 1 end)
else (case when convert(varchar(5),birth_date,1) between convert(varchar(5),@date-7,1) and convert(varchar(5),@date,1)
then 1 else 0 end)
end)=1drop table [table]
如果7天前的日期(月日)小于这一天的(月日),则用between ... and ...
Create table [table]
(ID int,
birth_date DateTime)
--插入数据
Insert [table] Values(1,'1981/12/26')
Insert [table] Values(2,'1977/02/26')
Insert [table] Values(3,'1978/01/05')
Insert [table] Values(4,'1979/01/01')
Insert [table] Values(5,'1980/12/27')
Insert [table] Values(6,'1980/12/25')
SELECT * FROM [TABLE] WHERE DATEDIFF(day, birth_date,'1981/01/01')<71 1981-12-26 00:00:00.000
5 1980-12-27 00:00:00.000
SELECT * FROM ls_TABLE5 WHERE DATEDIFF(day, birth_date,GETDATE())>7 and year(getdate())-year(birth_date)<=1
Where DateDiff(dd,Convert(DateTime,Convert(Varchar(6),dateadd(dd,7,birth_date),110)+'05'),dateadd(dd,7,GetDate())<7 And
DateDiff(dd,Convert(DateTime,Convert(Varchar(6),dateadd(dd,7,birth_date),110)+'05'),dateadd(dd,7,GetDate())>=0