akkapolk
akkapolk

Reputation: 594

What is different between DATEADD(DAY, 1, GETDATE()) and DATEADD(DAY, 1, DATEDIFF(DAY, 0, GETDATE()))

What is different between

DATEADD(DAY, 1, GETDATE())

and

DATEADD(DAY, 1, DATEDIFF(DAY, 0, GETDATE()))

Could someone help to show example case, how to use them?

Upvotes: 1

Views: 9303

Answers (2)

Bhargav J Patel
Bhargav J Patel

Reputation: 166

In your first Version DateAdd() Adding Date in Current Date.

In your Second Version first Execute DATEDIFF(DAY, 0, GETDATE()) It Gives you Date Different and After that It Will Add One Day in DATEDIFF(DAY, 0, GETDATE()) Result.

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269603

The first version includes the time component of GETDATE(). The second does not. So, if the current time is 2018-01-01T05:43:26, then the first version returns:

2018-01-02T05:43:26

The second removes the time component, so it returns:

2018-01-02T00:00:00

I think a better version to get midnight when the next day starts is:

dateadd(day, 1, cast(getdate() as date))

Upvotes: 6

Related Questions