Solved

Last Sat of the Previous month

Posted on 2007-11-20
7
335 Views
Last Modified: 2012-08-14
Hello,

How can I calculate the last Saturday of the previous month?


Thanks !
0
Comment
Question by:sbagireddi
  • 3
  • 3
7 Comments
 
LVL 25

Expert Comment

by:imitchie
ID: 20324734
declare @YOUR_DATE datetime
set @YOUR_DATE=CONVERT(datetime, '2003/07/01')
select DATEADD(d, (CASE WHEN DATEPART(dw, @YOUR_DATE)>=6 THEN 6-DATEPART(dw, @YOUR_DATE) ELSE -DATEPART(dw, @YOUR_DATE) END), @YOUR_DATE)
0
 
LVL 19

Expert Comment

by:frankytee
ID: 20324775
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'
0
 
LVL 8

Author Comment

by:sbagireddi
ID: 20324779
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!

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 19

Accepted Solution

by:
frankytee earned 250 total points
ID: 20324784
if you want to get remove the time component:
declare @dtX as smalldatetime
--last day of last month
set @dtx = cast(floor(cast((getdate() -(day(getdate()))) as int)) as smalldatetime)
--now last saturday
select
   case DATEPART ( dw , @dtx )
       when 6 then @dtx
        else @dtx - DATEPART( dw , @dtx )
   end as 'LastSaturdayOfPreviousMonth'
0
 
LVL 25

Assisted Solution

by:imitchie
imitchie earned 250 total points
ID: 20324836
sorry, fixed

select dateadd(d, -

 isnull(nullif(

 datepart(dw, dateadd(m, month(@your_date)-1, dateadd(yy, year(@your_date) - 1900, 0))-1),7),0),

 dateadd(m, month(@your_date)-1, dateadd(yy, year(@your_date) - 1900, 0))-1)

Open in new window

0
 
LVL 25

Expert Comment

by:imitchie
ID: 20324853
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)
0
 
LVL 19

Expert Comment

by:frankytee
ID: 20324958
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 'LastSaturdayOfPreviousMonth'
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now