How to find data from this day exactly a year ago?

Now I have:

year(epe_curremploymentdate) = year(DATEADD(year, -1, SYSDATETIME())) 

But that gives me everything from last year. All I want is data from today, a year ago.

+4
source share
5 answers

It should be:

epe_curremploymentdate = DATEADD(year, -1, GETDATE())

Do not check the year in the where clause.

EDIT:

Simple: use

cast(epe_curremploymentdate AS DATE) = cast(DATEADD(year, -1,
  GETDATE()) AS DATE)

That is, if you are using SQL Server 2008 and above.

+9
source

This should lead you where you need to:

Declare @StartDate datetime, @EndDate datetime

-- @StartDate is midnight on today date, last year
set @StartDate = Convert(date, (DATEADD(year, -1, getdate()))) 
set @EndDate = DATEADD(Day, 1, @StartDate)

select * 
from YourTable 
where epe_curremploymentdate >= @StartDate 
    and epe_curremploymentdate < @EndDate

-- This where clause will get you everything that happened at any time 
-- on today date last year.
+3
source

, - 00: 00: 00.000, ,

 dateadd(yy,-1,datediff(d,0,getdate())) 
+1

:

select getdate()

:

select dateadd(year, -1, getdate())

:

select dateadd(d, -1 , dateadd(year, -1, getdate()))

UPDATE

select *
from
epe_curremploymentdate = dateadd(year, -1, CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME))

, .

, !!!

0
source

epe_curremploymentdate = DATEADD (year, -1, GETDATE ())

0
source

Source: https://habr.com/ru/post/1545431/


All Articles