how to get yesterday date in sql
SELECT DATEADD(day, -1, CAST(GETDATE() AS date)) AS YesterdayDate;
how to get yesterday date in sql
SELECT DATEADD(day, -1, CAST(GETDATE() AS date)) AS YesterdayDate;
comparing with today and yesterday record in sql query
declare @bgn_dt date = '2017-12-15' --set by OP
, @end_dt date = '2017-12-22' --set by OP
, @lag_dt date;
set @lag_dt = (select max(MyDate) from #myTable where MyDate < @bgn_dt) --get the "yesterday" that the @bgn_dt will need
select a.MyDate
, a.SalesTotal
, format(((1.0 * a.SalesTotal) / a.SalesTotalPrevDay) - 1, '0%') as SalesTotalChange
from (
select t.MyDate
, t.SalesTotal
, lag(t.SalesTotal, 1, NULL) over (/*partition by (if needed)*/ order by t.MyDate asc) as SalesTotalPrevDay
from #myTable as t
where 1=1
and t.MyDate between @lag_dt and @end_dt
) as a
where 1=1
and a.MyDate >= @bgn_dt
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us