Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8560887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:22:46+00:00 2026-06-11T16:22:46+00:00

I have to calculate date before two days based on the condition . Example

  • 0

I have to calculate date before two days based on the condition .

Example date = ‘2012-09-01’ then @date should be ‘2012-08-30’ but here if 2012-08-31 is not working day then @date should be ‘2012-08-29’.

Could you please help me on this

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:22:47+00:00Added an answer on June 11, 2026 at 4:22 pm

    Your question though apparently seems to be simple, but not so as there are many test cases involved in it (at least I have interpreted in that way). Let me first write the query (I assume you are using Sql Server 2005+)

    Declare @tbl_Data Table(Dates Date,WorkDayCount Int)
    
    Insert Into @tbl_Data Select '2012-08-25',1
    Insert Into @tbl_Data Select '2012-08-26',1
    Insert Into @tbl_Data Select '2012-08-27',1
    Insert Into @tbl_Data Select '2012-08-28',1
    Insert Into @tbl_Data Select '2012-08-29',1
    Insert Into @tbl_Data Select '2012-08-30',1
    Insert Into @tbl_Data Select '2012-08-31',0
    Insert Into @tbl_Data Select '2012-09-01',1
    
    Declare @InputData Date = '2012-09-01'
    
    ;WITH CTE AS
    (
        SELECT Sequence = ROW_NUMBER() OVER(ORDER BY Dates DESC),*
        FROM @tbl_Data
        WHERE  Dates < @InputData
    )
    ,CTE2 AS
    (
        SELECT 
            Sequence = ROW_NUMBER() OVER(ORDER BY Dates DESC)
            ,Dates 
            ,WorkDayCount
        FROM
        (
            SELECT * FROM CTE WHERE Sequence  = ((SELECT TOP 1 Sequence FROM CTE WHERE WorkDayCount = 1)-1)
            UNION  
            SELECT TOP 2 * FROM CTE WHERE WorkDayCount = 1
        )X
    )
    SELECT TOP 1
                Date = CASE WHEN  Sequence = 1 AND  WorkDayCount = 0 THEN (SELECT Dates FROM CTE2 WHERE  Sequence = 3)
                        WHEN  Sequence = 1 AND  WorkDayCount = 1 THEN (SELECT Dates FROM CTE2 WHERE  Sequence = 2)
                END 
    FROM CTE2
    

    CASE 1:Input date is 1st Sept(2012-09-01) and the immediate previous date’s WorkDayCount = 0

    Result: 2012-08-29

    Reason: Since August 31st’s WorkDayCount = 0, so the working date will be 29th August

    CASE 2: Input date is 1st Sept(2012-09-01) and the August 31st + August 30th WorkCount = 0.

    Result: 2012-08-28

    CASE 3:Input date is 1st Sept(2012-09-01) and the immediate previous date’s WorkDayCount = 1

    Result: 2012-08-30

    Kindly correct me if any of the assumptions/test cases are incorrect.

    Let me know your concern.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two fields which calculate a total, but I have a radio button
Does anyone have a good algorithm to calculate what date Good Friday falls on
I have a method to calculate the exponent, but it doesnt like the c
I have this query to calculate the most common days of the week and
I have a date such as 2009-06-30 (30th june, 2009). I want to calculate
Possible Duplicate: Calculate years from date Hi, I have a table with a field
I want to calculate the start date of the last month. I have referred
I have a table containing as inputs the Date, Waketime, and Bedtime. Based on
I have to calculate the Specular Highlights (phong) of an Image. the normal Vector
I have to calculate the average of each column in a specific nested list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.