Solved

# Last Sat of the Previous month

Posted on 2007-11-20
338 Views
Hello,

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

Thanks !
0
Question by:sbagireddi
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 3

LVL 25

Expert Comment

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

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

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

LVL 19

Accepted Solution

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

imitchie earned 250 total points
ID: 20324836
sorry, fixed

``````select dateadd(d, -
isnull(nullif(
``````
0

LVL 25

Expert Comment

ID: 20324853
this part is repeated:
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

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

Question has a verified solution.

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

### Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
###### Suggested Courses
Course of the Month6 days, 19 hours left to enroll