Arianule
Arianule

Reputation: 9053

Retrieving time period t-sql

I have a stored procedure set up in such a way as to get the results for today

so

DECLARE @Start DATETIME
DECLARE @End DATETIME
SET @Start = DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
SET @End = DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 1)

PRINT @Start
PRINT @End

gives me the time period

May 30 2016 12:00AM
May 31 2016 12:00AM

How can I modify the clause as to give me the period

 May 29 2016 5:30PM(Previous day)
 May 31 2016 12:00AM(Today)

Kind regards

Upvotes: 0

Views: 50

Answers (3)

iladan
iladan

Reputation: 151

Try this solution:

SET @Start = DATEADD(MINUTE, 30, DATEADD(HOUR, 17, DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), -1)))

SET @End = DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 1)

Upvotes: 1

haku
haku

Reputation: 4505

dateadd(day, -1, getdate())

May be just use the DateAdd like above.

Upvotes: 1

Shilpa Soni
Shilpa Soni

Reputation: 2142

DECLARE @Start DATETIME
DECLARE @End DATETIME
SET @Start = GETDATE()-1
SET @End = DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 1)

PRINT @Start
PRINT @End

Upvotes: 1

Related Questions