sbagireddi
asked on
Last Sat of the Previous month
Hello,
How can I calculate the last Saturday of the previous month?
Thanks !
How can I calculate the last Saturday of the previous month?
Thanks !
declare @dtX as smalldatetime
set @dtx = getdate() -(day(getdate()))
select case DATEPART ( dw , @dtx )
when 6 then @dtx
else @dtx - DATEPART( dw , @dtx )
end as 'LastSaturday'
set @dtx = getdate() -(day(getdate()))
select case DATEPART ( dw , @dtx )
when 6 then @dtx
else @dtx - DATEPART( dw , @dtx )
end as 'LastSaturday'
ASKER
Hi,
When I substitute the @YOUR_DATE with getdate( ) it gives the last Sat ie. 11/17/2007.
I would like to know the last Sat of the previous month i.e Oct, it would be 10/27/2007.
Let me explain the context:
I am recursing through a directory and want to know all the log backups in the directory from the last Sat of the previous month (when the last full backup completes) to the first day of the current month.
Thanks a ton!
When I substitute the @YOUR_DATE with getdate( ) it gives the last Sat ie. 11/17/2007.
I would like to know the last Sat of the previous month i.e Oct, it would be 10/27/2007.
Let me explain the context:
I am recursing through a directory and want to know all the log backups in the directory from the last Sat of the previous month (when the last full backup completes) to the first day of the current month.
Thanks a ton!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
this part is repeated:
dateadd(m, month(@your_date)-1, dateadd(yy, year(@your_date) - 1900, 0))-1,
which essentially is one of 100 ways to get "last day of last month"
if the day of week is not 7 (Sat), we need to make it 7, we do this by simply taking the day of week off! i.e. Wed is 4, so we take 4 days off to make .. Sat. The trick is that IF it is already Sat, we take none off, hence isnull(nullif(@x, 7), 0) so that 7 becomes 0.
depends on @@datefirst being 1 for Sunday (pretty much all English dbs)
dateadd(m, month(@your_date)-1, dateadd(yy, year(@your_date) - 1900, 0))-1,
which essentially is one of 100 ways to get "last day of last month"
if the day of week is not 7 (Sat), we need to make it 7, we do this by simply taking the day of week off! i.e. Wed is 4, so we take 4 days off to make .. Sat. The trick is that IF it is already Sat, we take none off, hence isnull(nullif(@x, 7), 0) so that 7 becomes 0.
depends on @@datefirst being 1 for Sunday (pretty much all English dbs)
typo in mine, it should have been 7 instead of a 6.
declare @dtX as smalldatetime
set @dtx = getdate() -(day(getdate()))
select
case DATEPART ( dw , @dtx )
when 7 then @dtx
else @dtx - DATEPART( dw , @dtx )
end as 'LastSaturdayOfPreviousMon th'
declare @dtX as smalldatetime
set @dtx = getdate() -(day(getdate()))
select
case DATEPART ( dw , @dtx )
when 7 then @dtx
else @dtx - DATEPART( dw , @dtx )
end as 'LastSaturdayOfPreviousMon
set @YOUR_DATE=CONVERT(datetim
select DATEADD(d, (CASE WHEN DATEPART(dw, @YOUR_DATE)>=6 THEN 6-DATEPART(dw, @YOUR_DATE) ELSE -DATEPART(dw, @YOUR_DATE) END), @YOUR_DATE)