3

我正在尝试使用以下代码列出本周过生日的人:

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)
  )

首先我设置开始日期并根据开始日期计算结束日期。然后我将星期一设置为一周的第一天。但是每次我运行它时,我都会收到一个不同的开始日期。我对 Ms Sql Scripting 很陌生,我可能在声明中做错了什么,但我找不到它。提前致谢。

4

5 回答 5

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)
于 2012-06-27T08:23:57.093 回答
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))
于 2012-06-27T08:34:07.860 回答
1

sql应该让你直接指定星期

DATEPART(ww,GETDATE()) 

或在甲骨文中: to_char( mydate, 'ww' )

于 2012-06-27T09:00:35.130 回答
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)

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

于 2012-06-27T11:43:35.237 回答
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 进行搜索。

如果新的一年开始的一周中途并且您想要的生日在新的一年,这可能仍然会失败。

于 2012-06-27T12:13:30.470 回答