查找本周过生日的人员名单

时间:2012-06-27 08:17:21

标签: sql sql-server-2008

我试图用这段代码列出本周过生日的人:

declare @START_DATE date;
set @START_DATE  = DATEADD(dd, 1 - DATEPART(dw, getdate()), getdate())
declare @END_DATE date;
set @END_DATE = DATEADD(dd,6, @START_DATE)
set DATEFIRST 1

SELECT  @START_DATE as StartDate, Personel.DogumTarihi , @END_DATE as EndDate
      ,[Adi]
      ,[Soyadi]
      ,[BirimAdi]
      ,[DogumTarihi]
      ,[MudurlukAdi]
      ,[Gorevi]
      ,[CepTelefonu]
      ,[EvTelefonu]
from Personel 
where (
      DATEPART(m, Personel.DogumTarihi) = DATEPART(m, @START_DATE) 
  and DATEPART(DAY, Personel.DogumTarihi) >= DATEPART(DAY, @START_DATE) 
  and DATEPART(DAY, Personel.DogumTarihi) <= DATEPART(DAY, @END_DATE)
  ) 
  OR (
      DATEPART(m, Personel.DogumTarihi) = (DATEPART(m, @START_DATE)+ 1) 
  and DATEPART(DAY, Personel.DogumTarihi) <= DATEPART(DAY, @END_DATE)
  )

首先,我根据开始日期设置开始日期并计算结束日期。然后我将星期一定为一周的第一天。 但每次我运行这个,我都会收到一个不同的开始日期。我是Sql Scripting女士的新手,我可能在声明中做错了但是我找不到它。提前谢谢。

5 个答案:

答案 0 :(得分:2)

好的,还有一次尝试:

where

case when year(@START_DATE) = year(@END_DATE) THEN '1' ELSE CASE WHEN month(@birthdate) = 12 THEN '0' WHEN month(@birthdate) = 1 THEN '1' ELSE '9' END END
+ RIGHT('0' + CONVERT(varchar(2), MONTH(@birthdate)),2)
+ RIGHT('0' + CONVERT(varchar(2), DAY(@birthdate)),2)

between

case when year(@START_DATE) = year(@END_DATE) THEN '1' ELSE '0' END
+ RIGHT('0' + CONVERT(varchar(2), MONTH(@START_DATE)),2)
+ RIGHT('0' + CONVERT(varchar(2), DAY(@START_DATE)),2)

and

'1'
+ RIGHT('0' + CONVERT(varchar(2), MONTH(@END_DATE)),2)
+ RIGHT('0' + CONVERT(varchar(2), DAY(@END_DATE)),2)

答案 1 :(得分:2)

试试这个:

SELECT  DATEADD(WEEK, DATEDIFF(DAY, 0, GETDATE())/7, 0) FirstDayOfCurrentWeek, 
        DATEADD(YEAR, DATEDIFF(YEAR, DogumTarihi, GETDATE()), [DogumTarihi]) BirthdayThisYear,
        DATEADD(WEEK, 1, DATEADD(WEEK, DATEDIFF(DAY, 0, GETDATE())/7, 0)) FirstDayOfNextWeek           
          ,[Adi]
          ,[Soyadi]
          ,[DogumTarihi]
          ,[BirimAdi]
          ,[MudurlukAdi]
          ,[Gorevi]
          ,[CepTelefonu]
          ,[EvTelefonu]         
FROM    Personel
WHERE   DATEADD(YEAR, DATEDIFF(YEAR, [DogumTarihi], GETDATE()), [DogumTarihi]) >= DATEADD(WEEK, DATEDIFF(DAY, 0, GETDATE())/7, 0)
        AND
        DATEADD(YEAR, DATEDIFF(YEAR, [DogumTarihi], GETDATE()), [DogumTarihi]) < DATEADD(WEEK, 1, DATEADD(WEEK, DATEDIFF(DAY, 0, GETDATE())/7, 0))

答案 2 :(得分:1)

sql应该让你直接指定一周

DATEPART(ww,GETDATE()) 

或在oracle:to_char( mydate, 'ww' )

答案 3 :(得分:1)

为了解决我的问题,我根据答案修改了上面的代码:

set DATEFIRST 1
declare @START_DATE date;
set @START_DATE  = DATEADD(dd, 1 - DATEPART(dw, getdate()), getdate())
declare @END_DATE date;
set @END_DATE = DATEADD(dd,6, @START_DATE)


SELECT  @START_DATE as StartDate, 

(case when 
(DATEPART(m, @START_DATE) = DATEPART(m, @END_DATE))
then 
(case when (DATEPART(DAY, Personel.DogumTarihi) >= DATEPART(DAY, @START_DATE) and DATEPART(DAY, Personel.DogumTarihi) <= DATEPART(DAY, @END_DATE)) then Personel.DogumTarihi else 0 end)
else 
    (case when DATEPART(m, Personel.DogumTarihi) = DATEPART(m, @START_DATE)
        then
            (case when (DATEPART(DAY, Personel.DogumTarihi) >= DATEPART(DAY, @START_DATE)) then Personel.DogumTarihi else 0 end)
        else
            (case when (DATEPART(DAY, Personel.DogumTarihi) <= DATEPART(DAY, @END_DATE)) then Personel.DogumTarihi else 0 end)
        end
    )
end), 
@END_DATE as EndDate, [TCKN]
      ,[Adi]
      ,[Soyadi]
      ,[BirimAdi]
      ,[DogumTarihi]
      ,[MudurlukAdi]
      ,[Gorevi]
      ,[CepTelefonu]
      ,[EvTelefonu]
      ,[VakifTelefonu]
from Personel where 
DATEPART(m, @START_DATE) = DATEPART(m, @END_DATE)
OR DATEPART(m, Personel.DogumTarihi) = DATEPART(m, @START_DATE)
OR DATEPART(m, Personel.DogumTarihi) = DATEPART(m, @END_DATE)

现在考虑的是“月份的开始和结束日期在同一周(如本周)”。看起来它正在带来正确的结果..

答案 4 :(得分:1)

我们来试试吧:

declare @today datetime

set @today = getdate()
-- erase the hours
set @today = dateadd(hh, -datepart(hh, @today), @today)
-- erase the minutes
set @today = dateadd(mi, -datepart(mi, @today), @today)
-- erase the seconds    
set @today = dateadd(ss, -datepart(ss, @today), @today)
-- erase the milliseconds    
set @today = dateadd(ms, -datepart(ms, @today), @today)

-- go to start of current week (sunday!)    
set @today = dateadd(dd, 1-datepart(dw, @today), @today)

select * from MYTABLE
where DATECOL between 
  dateadd(yy, datepart(yy, DATECOL) - datepart(yy, @today), @today) and 
  dateadd(d, 7, dateadd(yy, datepart(yy, DATECOL) - datepart(yy, @today), @today))

显然SqlServer 2012有一个DATEFROMPARTS函数,在2005年我们将不得不使用这种迂回方式来修剪日期。

为了选择一个生日,我将“今天的一年”换成记录年份。对于范围的结束日期,我添加了7天。请注意,您希望从星期日0:00到星期六23:59进行搜索。

如果在新的一年开始的一周中途和你想要的生日是在新的一年中,这可能仍会失败。