Getdate(), -1 day
Asked Answered
G

6

27

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)) 
Guenna answered 12/1, 2016 at 8:33 Comment(4)
What does "doesn't work" look like?Forefinger
Do you actually need the CAST there?Biographical
I am not sure if that CAST is necessary. That's why I am asking help to make this better or fix this query line.Guenna
I figured that maybe this way is even better DATEADD(day,DATEDIFF(day,-1,GETDATE()),-1)Guenna
L
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

Lustrate answered 12/1, 2016 at 8:49 Comment(0)
S
17

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

getDate()-1

The function substracts (days) as standard.

Snowbird answered 29/10, 2019 at 13:31 Comment(1)
This will be a DATETIME value, with hours and minutes, not a DATE value which is just year/month/day.Shigella
N
8

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

 CAST(GETDATE()-1 as date )
Narbonne answered 10/4, 2020 at 11:19 Comment(0)
P
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

Patch answered 11/5, 2017 at 14:20 Comment(0)
F
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))
Fikes answered 21/2, 2019 at 16:40 Comment(0)
E
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)
Epistemic answered 14/5, 2020 at 18:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.