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

  • SEARCH
  • Home
  • 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 8942371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:29:52+00:00 2026-06-15T11:29:52+00:00

So I have a stored procedure that is working except for how it calculates

  • 0

So I have a stored procedure that is working except for how it calculates a certain field. On the tables worked with is a field called DeliveryYearMonth that is simply YYYYMM in its format. I try to compare current month info to previous month info, thus I created the following two variables.

DECLARE @CURDate VARCHAR(50) -- Current time (YYYYMM)
DECLARE @MAGDate VARCHAR(50) -- YearAgo Time (YYYYMM)

SET @CURDate = cast((select max(DeliveryYearMonth) from dbo.datatable) as  varchar)
SET @MAGDate = cast((substring(@CURDate,1,4)) as varchar) --For current year
         + cast((substring(@CURDate,5,2) - 1) as varchar) -- To get previous month

This is all fine and dandy unless the the month is January, and thus the month would become 00, and the year would stay the same.

So I tried my hand at an if statement to clear this up.

if(substring(@MAGDate,5,2)) = '00'-- If current Month is January
begin
Set @MAGDate = cast((substring(@CURDate,1,4) - 1) as varchar) --For previous year
         + '12' -- December
end

This always breaks the Month Ago Date (@MAGDate) even if the IF statement isn’t true. I cannot figure out why.

Thanks

  • 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-15T11:29:54+00:00Added an answer on June 15, 2026 at 11:29 am

    To get the previous version of your string, you can do the following:

    select replace(convert(varchar(7), dateadd(month, -1, CAST(@CURDate+'01' as DATE)), 121), '-', '')
    

    As a comment suggested, though, you should really just store this as a date, assuming you are using SQL Server 2008 or more recent.

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

Sidebar

Related Questions

I have a stored procedure that has been working just fine, but I recently
I have a stored procedure that takes a user ID and calculates their balance
I have the following stored procedure that I working on. I have noticed that
I have a stored procedure that is working with a large amount of data.
I have stored procedure that insanely times out every single time it's called from
Working with SSRS and a SQL 2000 source, I have a stored procedure that
I have written a single stored procedure that returns 2 tables: select * from
I have a stored procedure that is working well, it's a select query. I
I'm working with sql server 2008r2 developer. I have a stored procedure that adds
Working in SQL Server 2005, I have a stored procedure that inserts a record

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.