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 6818093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:08:03+00:00 2026-05-26T21:08:03+00:00

In Sql Server, I wrote a procedure for getting last 3 years, which includes

  • 0

In Sql Server, I wrote a procedure for getting last 3 years, which includes this year, and it returns only year [ like 2011,2010,2009]. This is my procedure. Is there any optimisation required? Please check the procedure.

       create procedure UPS_GetYears
      as
       begin
        declare @date varchar(10)
        declare @i int=0
        declare @var varchar(60)
        declare @y int =-1
        select @date = SUBSTRING (CONVERT (varchar,GETDATE(),103),7,4)         
        create table #Temp (years varchar(10))
        while (@i < 3)
        begin           
         insert into #Temp (years) values(@date )           
         select @date = SUBSTRING (convert(varchar, DATEADD (yyyy,@y,getdate()),103),7,4)
         set @i=@i+1
         set @y =@y-1        
        end    
      select * from #Temp 
     end
  • 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-05-26T21:08:03+00:00Added an answer on May 26, 2026 at 9:08 pm

    I’d just do

    SELECT DATEPART(YEAR, GETDATE()) - N AS years
    FROM   (VALUES(0),
                  (1),
                  (2)) T(N)  
    

    Also why is this a procedure rather than a TVF?

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

Sidebar

Related Questions

In Sql Server 2005, I have a stored procedure, in which i have wrote
I write a stored procedure in SQL Server like this select distinct top 5
I'm trying to write this as part of stored procedure on SQL Server 2000.
Is there a way to write SQL Server Stored Procedure which to be strongly
I'm working on converting a stored procedure from SQL server to Oracle. This stored
I write the following stored procedure in sql server 2008 i am getting the
I want to write a SQL Server 2005 stored procedure which will select and
I'd like to write a stored procedure and store it in a SQL Server
I need to write a Stored procedure in SQL server whose data returned will
I wrote this procedure in a site. it take a string as input parameter(user

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.