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: Generate Calendar Data for 19th Century

SQL Puzzle: Generate Calendar Data for 19th Century

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

Check the below input data and expected output for generating the complete calendar data for 19th century.

Input Date: 19000101

Expected Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
CalendarDate
------------
1900-01-01
1900-01-02
1900-01-03
1900-01-04
1900-01-05
1900-01-06
1900-01-07
1900-01-08
1900-01-09
.....
.....
.....
2000-12-25
2000-12-26
2000-12-27
2000-12-28
2000-12-29
2000-12-30
2000-12-31

Solution:

1
2
3
4
5
SELECT TOP (36890)
CAST(DATEADD(day, ROW_NUMBER() OVER (ORDER BY a.[object_id])-1, '19000101') AS DATE) AS CalendarDate
FROM sys.all_objects AS a
CROSS JOIN sys.all_objects AS b
OPTION (MAXDOP 1);

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

May 15, 2018Anvesh Patel
SQL Puzzle: Use Recursive CTE, and list out the Years from DatesSQL Server Coding Standards: About Object Identifiers
Comments: 3
  1. Ovidiu-Ionel Samfirescu
    January 14, 2019 at 8:45 pm

    DECLARE @INPUTDATE INT = 19000101
    DECLARE @TABLE TABLE (CalendarDATE DATE)
    INSERT INTO @TABLE
    SELECT CAST(SUBSTRING(CAST(@INPUTDATE AS CHAR),1,4) + ‘-‘ + SUBSTRING(CAST(@INPUTDATE AS CHAR),5,2) + ‘-‘ + SUBSTRING(CAST(@INPUTDATE AS CHAR),7,8) AS DATE)
    WHILE (SELECT MAX(CalendarDATE) FROM @TABLE) < '2001-01-01'
    BEGIN
    INSERT INTO @TABLE
    SELECT DATEADD(DAY,1,(SELECT MAX(CalendarDATE) FROM @TABLE))
    DECLARE @PRINT NVARCHAR(100) = (SELECT CAST(MAX(CalendarDATE) AS NVARCHAR) FROM @TABLE)
    PRINT @PRINT
    END
    SELECT * FROM @TABLE

    ReplyCancel
  2. Kati Novikov
    February 12, 2019 at 6:04 pm

    declare @dt date = ‘1900-01-01’
    create table #date (dt date);

    while @dt < getdate()
    begin
    insert into #date (dt) values (@dt);
    set @dt = DATEADD(day, 1, @dt);
    end

    select * from #date

    ReplyCancel
  3. HIC
    April 16, 2019 at 10:09 am

    WITH calendar(fdate) AS (
    SELECT to_date(‘01.01.1900’, ‘dd.mm.yyyy’) as fdate from dual
    UNION ALL
    SELECT fdate + 1 FROM calendar
    WHERE fdate + 1 <= sysdate
    )
    SELECT fdate FROM calendar;

    ReplyCancel

Leave a Reply to Ovidiu-Ionel Samfirescu Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

May 15, 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....