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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:09:07+00:00 2026-06-16T21:09:07+00:00

I want to display 12 months name from sql server. i though to accomplish

  • 0

I want to display 12 months name from sql server. i though to accomplish insert month name into temp table and then fire select statement on that table. so i had to write 12 insert table to insert 12 months name. so i search google to find better solution and i got it.

here is the sql statement

WITH R(N) AS
(
    SELECT 0
    UNION ALL
    SELECT N+1 
    FROM R
    WHERE N < 12
)

SELECT LEFT(DATENAME(MONTH,DATEADD(MONTH,-N,GETDATE())),3) AS [month]
FROM R

the above script works perfectly but my problem is i just do not understand how it works. i never work with CTE.

so tell me what is the meaning of WITH R(N) AS

and see this sql

SELECT LEFT(DATENAME(MONTH,DATEADD(MONTH,-N,GETDATE())),3) AS [month] FROM R
when above sql execute how it is getting value for -N ??
because here i have not set anything for -N ??

so please anyone help me to understand how whole thing works. thaks

My Second Phase of Question

just have look a and tell me

;WITH months(MonthNumber) AS
(
    SELECT 0
    UNION ALL
    SELECT MonthNumber+1 
    FROM months
    WHERE MonthNumber < 12
)

onething is not clear to me that why only first time the below part execute

SELECT 0
UNION ALL
SELECT MonthNumber+1 
FROM months
WHERE MonthNumber < 12

and from the 2nd time only this below part execute

SELECT MonthNumber+1 
FROM months
WHERE MonthNumber < 12

whenever we write two sql statement using Union and execute then always it return data from two sql state but specially in this case from the 2nd time why only this below part
execute

SELECT MonthNumber+1 
FROM months
WHERE MonthNumber < 12

basically i not familiar with CTE with recursion technique and that is why things is not getting clear to me. if possible please discuss how CTE recursion works.

DECLARE @TotaDays SMALLINT
DECLARE @Month VARCHAR(15)
DECLARE @Year SMALLINT
DECLARE @date DATETIME 
SET @Month = 'January'
SET @Year = 2015
SET @date = '01 ' + @Month + ' ' + CONVERT(VARCHAR(4),@Year)
SET @TotaDays = 0
SELECT @TotaDays = DATEDIFF(DAY, @date, DATEADD(MONTH, 1, @date))

;WITH months(MonthNumber) AS
(
    SELECT 1
    UNION ALL
    SELECT MonthNumber+1 
    FROM months
    WHERE MonthNumber < @TotaDays
)

select * from months;
  • 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-16T21:09:08+00:00Added an answer on June 16, 2026 at 9:09 pm

    The With R(N) is a Common Table Expression. From MDSN:

    A common table expression (CTE) can be thought of as a temporary
    result set that is defined within the execution scope of a single
    SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement. A CTE is
    similar to a derived table in that it is not stored as an object and
    lasts only for the duration of the query. Unlike a derived table, a
    CTE can be self-referencing and can be referenced multiple times in
    the same query.

    The R is the name of the result set (or table) that you are generating. And the N is the month number.

    This CTE in particular is a Recursive Common Table Expression. From MSDN:

    A common table expression (CTE) provides the significant advantage of
    being able to reference itself, thereby creating a recursive CTE. A
    recursive CTE is one in which an initial CTE is repeatedly executed to
    return subsets of data until the complete result set is obtained.

    When using CTE my suggestion would to be more descriptive with the names. So for your example you could use the following:

    ;WITH months(MonthNumber) AS
    (
        SELECT 0
        UNION ALL
        SELECT MonthNumber+1 
        FROM months
        WHERE MonthNumber < 12
    )
    select *
    from months;
    

    In my version the months is the name of the result set that you are producing and the monthnumber is the value. This produces a list of the Month Numbers from 0-12 (See Demo).

    Result:

    | MONTHNUMBER |
    ---------------
    |           0 |
    |           1 |
    |           2 |
    |           3 |
    |           4 |
    |           5 |
    |           6 |
    |           7 |
    |           8 |
    |           9 |
    |          10 |
    |          11 |
    |          12 |
    

    Then the SELECT statement immediately after is using the values of the CTE result set to get you the Month Names.

    Final query (See Demo):

    ;WITH months(MonthNumber) AS
    (
        SELECT 0
        UNION ALL
        SELECT MonthNumber+1 
        FROM months
        WHERE MonthNumber < 12
    )
    SELECT LEFT(DATENAME(MONTH,DATEADD(MONTH,-MonthNumber,GETDATE())),3) AS [month]
    FROM months;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the SQL Server Full-Text Indexing scheme i want to know if a table
I want to display the previous month name. My code is given below but
I want to display the last March month. I am using the following code,
I need to display a chart showing per-month sales; I want to display the
I want display data from database in Listbox...Here is my code, It is not
i want display 1 record from colums zodys , I'm programint in C# I
I have db with this table (TableToDo): http://goo.gl/NlTEk I want display all records in
I have a mySQL table of events and want to display them in a
I want to know how to display report by month using PHP and Mysql
My app is for expenses in every month, I have table with name Expense

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.