Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2018 May SQL Puzzle: Use Recursive CTE, and list out the Years from Dates

SQL Puzzle: Use Recursive CTE, and list out the Years from Dates

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Check the below input data and expected output for getting the list of years between given dates. Use recursive CTE to achieve this.

Input Date Range: 2010-01-01 to 2018-01-01
Expected Output:

1
2
3
4
5
6
7
8
9
10
11
Years
-----------
2010
2011
2012
2013
2014
2015
2016
2017
2018

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
DECLARE @Start_Date date = '20100101', @End_Date date = '20180101';
 
;WITH cte(DT) AS
(
SELECT DT = @Start_Date
UNION ALL
SELECT DATEADD(YEAR, 1, DT) FROM cte
WHERE DATEADD(YEAR, 1, DT) <= @End_Date
)
SELECT YEAR(DT) AS Years FROM cte
ORDER BY DT
OPTION (MAXRECURSION 3660);

Please try the different solution for this puzzle and share it via comment...

May 14, 2018Anvesh Patel
SQL Server: Set the default value to IMAGE Column or Binary DataSQL Puzzle: Generate Calendar Data for 19th Century
Comments: 3
  1. Dinesh
    May 26, 2018 at 8:08 am

    Declare @StartDate DateTime,@EndDate DateTime;
    Set @StartDate=’20100101′
    Set @EndDate=’20180101′

    ;
    WITH CTE
    As
    (Select Year(Convert(DateTime,@StartDate,112)) As YearOfBIll
    Union All
    Select YearOfBIll+1
    From CTE
    Where YearOfBill<Year(Convert(DateTime,@EndDate,112))
    )
    Select * From CTE

    ReplyCancel
  2. Prakash Annadurai
    January 14, 2019 at 12:15 am

    Here is my solution

    Drop table if exists #Records
    Create table #Records
    (YearColumn date)

    INSERT INTO #Records
    VALUES
    (‘2004-01-01’)
    ,(‘2009-01-01’)
    ,(‘2012-01-01’)
    ,(‘2013-01-01’)
    ,(‘2016-01-01’)
    ,(‘2017-01-01’)

    With Res as
    (
    SELECT yearColumn, lead(YearColumn) over(Order by YearColumn) as NextVal
    From #Records
    )
    select Year(Dateadd(year,1,YearColumn)),Year(Dateadd(year,-1,NextVal))
    from Res
    Where datediff(year,YearColumn,NextVal)>1
    Order by YearColum

    ReplyCancel
  3. HIC
    April 17, 2019 at 9:59 am

    pl/sql version:

    with years(fyear) as (
    select extract(year from to_date(‘01.01.2010’, ‘dd.mm.yyyy’)) as fyear from dual
    union all
    select fyear + 1 from years where fyear < extract(year from to_date('01.01.2018', 'dd.mm.yyyy'))
    )
    select * from years;

    ReplyCancel

Leave a Reply to Dinesh Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

May 14, 2018 3 Comments SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, SQL Advance Query, SQL Interview, SQL Problem, SQL Programming, SQL Puzzle, SQL Query, SQL Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....