26

I do not understand why, but somehow this query doesn't work. I want to take system date -1 day where the sysdate is smaller by 1 day then current date.

WHERE
    a.SEND_Date >= dateadd(DD,-1,(CAST(getdate() as date) as datetime)) 
N2hvits
  • 329
  • 1
  • 3
  • 15

6 Answers6

41

The CAST depends on what kind of date type you need. If you need only to compare dates you can use only:

dateadd(DD, -1, cast(getdate() as date))

If you need to compare with date time you can use:

dateadd(DD,-1,getdate())

That wil give you datetime like this: 2016-01-11 10:43:57.443

Andy K
  • 4,944
  • 10
  • 53
  • 82
Yahel
  • 546
  • 5
  • 6
14

In T-SQL (sqlserver) you can simply do :

getDate()-1

The function substracts (days) as standard.

Obsidian
  • 3,719
  • 8
  • 17
  • 30
FSciacca
  • 174
  • 1
  • 5
7

Or you can try this without making it any more difficult?

 CAST(GETDATE()-1 as date )
Ivan Ivanov
  • 131
  • 1
  • 2
  • 6
0

The checked answer still has time (00:00:00), in my version anyway. To get DATE only use: Select Convert(date,dateadd(day, -1, getdate()))

Both have same weight, 0.001 seconds

user3806549
  • 1,428
  • 1
  • 17
  • 25
0

There's just one CAST missing:

dateadd(DD,-1,(CAST(getdate() as date) as datetime))

two times "as" (as date + as datetime) but only one time "CAST" => something wrong - should be:

dateadd(DD,-1,CAST(CAST(getdate() as date) as datetime))
Jérôme
  • 1,254
  • 2
  • 20
  • 25
rf0806
  • 1
0

Please Don't follow any as this date will come issue on 01/05/2020 will print as 00/05/2020. So kindly use this below for the fix

select CONVERT(varchar, DATEADD(DAY, -1, convert(Nvarchar, GETDATE(),112)),112)
Paul Roub
  • 36,322
  • 27
  • 84
  • 93