Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getdate(), -1 day

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)) 
like image 786
N2hvits Avatar asked Jan 12 '16 08:01

N2hvits


People also ask

How get day of a date in SQL?

Method 1: DateName() Function for Day Name DECLARE @DateVal DATE = '2020-07-27'; SELECT @DateVal As [Date], DATENAME(WEEKDAY, @DateVal) AS [Day Name];

What does date () do in SQL?

The date function DAY accepts a date, datetime, or valid date string and returns the Day part as an integer value.


2 Answers

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

like image 162
Yahel Avatar answered Sep 28 '22 03:09

Yahel


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

getDate()-1

The function substracts (days) as standard.

like image 29
FSciacca Avatar answered Sep 28 '22 03:09

FSciacca